*Ìܼ¡ [#q1036b05]

#contents

*Rails¤Ë¤è¤ë¥Æ¥¹¥ÈºîÀ® [#yb95687b]

http://www.pragmaticprogrammer.com/title/rails/
¤Î12¾Ï¡ÖTask T: Testing¡×¤È
http://jp.rubyist.net/magazine/?0013-RubyOnRails
¤ò»²¾È¤·¤Ê¤¬¤éRails¤Ë¤è¤ë¥æ¥Ë¥Ã¥È¥Æ¥¹¥È¡¢µ¡Ç½¥Æ¥¹¥È¡¢¼«Æ°²½¥Æ¥¹¥È¤ÎºîÀ®¤ò¹Ô¤Ã¤Æ¤ß¤ë

**½àÈ÷ [#l95a796f]

¥¹¥±¥ë¥È¥ó¤òºî¤ë

# rails gentest

¤³¤ÎÃʳ¬¤Çgentest/test/¤Ë¤Ï¥æ¥Ë¥Ã¥È¥Æ¥¹¥È¡¢µ¡Ç½¥Æ¥¹¥È¤Î¥¹¥±¥ë¥È¥ó¤âºîÀ®¤µ¤ì¤Æ¤¤¤ë

# cd gentest/test
# dir

 fixtures/
 functional/
 mocks/
 unit/
 test_helper.rb

fixtures¤Ï¥Æ¥¹¥ÈÍѥǡ¼¥¿¥Ù¡¼¥¹¤Î½é´ü²½»þ¤Ë½é´ü¥Ç¡¼¥¿¤È¤·¤ÆÅêÆþ¤¹¤ë¥Ç¡¼¥¿¤òÄêµÁ¤·¤¿YAML(*.yml)¥Õ¥¡¥¤¥ë¤ò³ÊǼ¤¹¤ë¥Ç¥£¥ì¥¯¥È¥ê¡¢functional¤Ïµ¡Ç½¥Æ¥¹¥È¤ò³ÊǼ¤¹¤ë¥Ç¥£¥ì¥¯¥È¥ê¡¢mocks¤Ï¥â¥Ã¥¯¤ò³ÊǼ¤¹¤ë¥Ç¥£¥ì¥¯¥È¥ê¡¢unit¤Ï¥æ¥Ë¥Ã¥È¥Æ¥¹¥È¤ò³ÊǼ¤¹¤ë¥Ç¥£¥ì¥¯¥È¥ê¤Ë¤Ê¤ë¡£test_helper.rb¤Ï¥Æ¥¹¥ÈÍѤζ¦ÄÌ´Ø¿ô¡¢ÄêµÁÃÖ¤­¾ì¡£

**¥â¥Ç¥ëÄêµÁ¤È¥Æ¥¹¥ÈºîÀ® [#u7932361]

¥â¥Ç¥ë¤òÄêµÁ¤·¤Æ¡¢¥Æ¥¹¥È¤òºîÀ®¤¹¤ë¡£

***½àÈ÷ [#a90f347e]

DB¥Æ¡¼¥Ö¥ë¤Ï²¼µ­¤ò»È¤¦(¥Æ¡¼¥Ö¥ë̾¡¢¥«¥é¥à̾¤ÏRails¤Îµ¬Ìó¤Ë½¾¤¦¡Ë

 -- books
 create table books (
   id                    integer primary key,
   title                 varchar(255) not null,
   description           text,
   updated_at            datetime
 );

¤³¤Î¥Æ¡¼¥Ö¥ë¥¹¥­¡¼¥Þ¤òdb/schema.sql¤È¤·¤ÆÊݸ¤·¡¢²¼µ­¤Ç¥Ç¡¼¥¿¥Ù¡¼¥¹¤òºîÀ®¤¹¤ë(SQLite¤ò»ÈÍÑ¡Ë

# cd db
# sqlite3 gentest.db < schema.sql

Àܳ¾ðÊó¤òconfig/database.yml¤Ëµ­ºÜ¤¹¤ë

 development:
   adapter: sqlite3
   dbfile: db/gentest.db

¥â¥Ç¥ë¥¯¥é¥¹(Book)¤Î¥¹¥±¥ë¥È¥ó¤òºîÀ®¤¹¤ë

# ruby script/generate model Book

¤³¤ÎÃʳ¬¤Ç¡¢
test/unit/book_test.rb
test/fixtures/books.yml
¤¬¼«Æ°ºîÀ®¤µ¤ì¤ë

book_test.rb¤ÏBook¥¯¥é¥¹ÍѤΥæ¥Ë¥Ã¥È¥Æ¥¹¥È¡¢books.yml¤ÏBook¥â¥Ç¥ëÍѤÎfixture¤Ë¤Ê¤ë¡£

***ºÇ½é¤Î¥Æ¥¹¥È¼Â¹Ô [#tfb0883a]

test/unit/book_test.rb¤Ï²¼µ­¤Î¤è¤¦¤Ë¤Ê¤Ã¤Æ¤¤¤ë

 require File.dirname(__FILE__) + '/../test_helper'
 
 class BookTest < Test::Unit::TestCase
   fixtures :books
 
   # Replace this with your real tests.
   def test_truth
     assert_kind_of Book, books(:first)
   end
 end

Rails¤Î¥Æ¥¹¥È¥¯¥é¥¹¤ÏTest::Unit::TestCase¥¯¥é¥¹¤Î¥µ¥Ö¥¯¥é¥¹¤Ë¤Ê¤Ã¤Æ¤¤¤ë¡£[[Test::Unit¥Õ¥ì¡¼¥à¥ï¡¼¥¯>http://www.ruby-lang.org/ja/man/?cmd=view;name=Test%3A%3AUnit]]¤ÏRails¤Ë¥Ð¥ó¥É¥ë¤µ¤ì¤Æ¤¤¤ë¤Î¤Ç¡¢¤³¤ÎÃʳ¬¤Ç¤¹¤°¤Ë¥Æ¥¹¥È¼Â¹Ô¤·¤Æ¤ß¤ë¤³¤È¤¬¤Ç¤­¤ë¡£

# ruby test/unit/book_test.rb

Rails¤Ïtest¥¹¥¯¥ê¥×¥È¤ò¼Â¹Ô¤¹¤ë¤¿¤Ó¤Ë¡¢¥Æ¥¹¥È¥Ç¡¼¥¿¥Ù¡¼¥¹¤òºîÀ®¤·¡¢¥Æ¥¹¥È¥á¥½¥Ã¥É¤ò¼Â¹Ô¤¹¤ë¤¿¤Ó¤Ëfixture¤Î¥Ç¡¼¥¿¤Ç¥Æ¥¹¥È¥Ç¡¼¥¿¥Ù¡¼¥¹¤Î¥Ç¡¼¥¿¤ò½é´ü²½¤¹¤ë¡£
¤·¤«¤·¡¢¤Þ¤À¥Æ¥¹¥È¥Ç¡¼¥¿¤Ù¡¼¥¹¤Ëdb/gentest.db¤Î¥¹¥­¡¼¥Þ¤òÄêµÁ¤·¤Æ¤¤¤Ê¤¤¤Î¤Ç¡¢¤³¤Î¸¡ºº¤Ï¼ºÇÔ¤¹¤ë¡£

¥Æ¥¹¥È¥Ç¡¼¥¿¥Ù¡¼¥¹¤Î¥¹¥­¡¼¥ÞÄêµÁ¤Ï²¼µ­¤Îrake¥³¥Þ¥ó¥É¤Ë¤è¤Ã¤Æ¼Â¹Ô¤¹¤ë

# rake clone_structure_to_test

¤³¤ì¤Ç¡¢¥¹¥­¡¼¥Þ¤À¤±¤¬¥Æ¥¹¥È¥Ç¡¼¥¿¥Ù¡¼¥¹¤ËÄêµÁ¤µ¤ì¤ë¡¦¡¦¡¦¤Ï¤º¤Ê¤Î¤À¤¬¡¢¼«Ê¬¤Î´Ä¶­¤Ç¤Ï²¼µ­¤Î¥¨¥é¡¼¤òÅǤ¤¤Æ¼ºÇÔ¤¹¤ë

 rake aborted!
 You have a nil object when you didn't expect it!
 You might have expected an instance of Array.
 The error occured while evaluating nil.[]

¤Á¤ç¤Ã¤ÈÄ´¤Ù¤Æ¤ß¤¿¤¬¡¢¸¶°ø¤Ï¤Þ¤ÀÉÔÌÀ¡£
¤Î¤Ç¡¢°Ê²¼¤Ï¼ÂºÝ¤ËÆ°ºî³Îǧ¤Ê¤·¤Î¾ðÊó¤Ë¤Ê¤ë

***fixtureÄêµÁ [#z20f23a3]

¥¹¥±¥ë¥È¥ó¾õÂ֤ǤÏtest/fixtures/books.yml¤ÎÆâÍƤϲ¼µ­¤ÎÄ̤ê

 # Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
 first:
   id: 1
 another:
   id: 2

id¤À¤±¤¬¥Ç¡¼¥¿¥»¥Ã¥È¤µ¤ì¤Æ¤¤¤ë¾õÂÖ¡£

fixture¤È¤·¤Æ²¼µ­¤òµ­½Ò¤¹¤ë

 first:
   id: 1
   title: ´ðÁ䫤é¤Ï¤¸¤á¤ëRuby
   description: ´ðÁ䫤é¤Ï¤¸¤á¤ëRuby¤ÎÀâÌÀ
 second:
   id: 2
   title: Effective Java
   desctiption: ¡Á¡Á¡Á¡Á¡Á¡Á¡Á

***¥â¥Ç¥ë¥ª¥Ö¥¸¥§¥¯¥ÈºîÀ®¤È¥ì¥³¡¼¥ÉÆɤ߹þ¤ß¤Î¥Æ¥¹¥È [#m1b5a227]

test/unit/book_test.rb¤Ësetup()¡¢test_create()¥á¥½¥Ã¥É¤ò²¼µ­¤Î¤è¤¦¤ËÄêµÁ¤¹¤ë~
¡ÊTest::Unit¤Ïtest_¤Ç»Ï¤Þ¤ë¥á¥½¥Ã¥É¤òÁ´¤Æ¼«Æ°¼Â¹Ô¤¹¤ë¡£¤Þ¤¿setup()¥á¥½¥Ã¥É¤Ï³Æ¥Æ¥¹¥È¥á¥½¥Ã¥É¤¬¸Æ¤Ð¤ì¤ëÁ°¤Ëɬ¤º¼Â¹Ô¤µ¤ì¤ë¡Ë

  def setup
	@book = Book.find(1)
  end
  
  def test_create
	assert_kind_of Book, @book
	assert_equal 1, @book.id
	assert_equal "´ðÁ䫤é¤Ï¤¸¤á¤ëRuby", @book.title
	assert_equal "´ðÁ䫤é¤Ï¤¸¤á¤ëRuby¤ÎÀâÌÀ", @book.description
  end

setup()¥á¥½¥Ã¥É¤Ç¤Ïid¤¬1¤ÎBook¥ì¥³¡¼¥É¤ò@book¤Ë³ÊǼ¤·¡¢³Æ¥Æ¥¹¥È¥á¥½¥Ã¥É¤Ç»²¾È¤Ç¤­¤ë¤è¤¦¤Ë¤·¤Æ¤¤¤ë¡£

test_create()¥á¥½¥Ã¥É¤Ç¤Ï¤Þ¤º¡¢@book¤¬Book¥â¥Ç¥ë¤Î¥ª¥Ö¥¸¥§¥¯¥È¤Ç¤¢¤ë¤³¤È¤ò³Îǧ¤·¡¢¤½¤Î¸å¥ª¥Ö¥¸¥§¥¯¥È¤Îid¡¢title¡¢descripton¤¬Àµ¤·¤¯¥»¥Ã¥È¤µ¤ì¤Æ¤¤¤ë¤³¤È¤ò³Îǧ¤·¤Æ¤¤¤ë¡£

***¥ì¥³¡¼¥É¹¹¿·¤Î¥Æ¥¹¥È [#sfd65f00]

test/unit/book_test.rb¤Ëtest_update()¥á¥½¥Ã¥É¤ò²¼µ­¤Î¤è¤¦¤ËÄêµÁ¤¹¤ë¡£

  def test_update
	assert_equal "´ðÁ䫤é¤Ï¤¸¤á¤ëRuby", @book.title
	@book.title = "¿·¡¦´ðÁ䫤é¤Ï¤¸¤á¤ëRuby";
	assert @book.save @book.errors.full_messages.join("; ")
	@book.reload
	assert_equal "¿·¡¦´ðÁ䫤é¤Ï¤¸¤á¤ëRuby", @book.title
  end

assert¥á¥½¥Ã¥É¤Ç¥Æ¥¹¥È¥Ç¡¼¥¿¥Ù¡¼¥¹¤Ø¤Î¹¹¿·¤¬Àµ¤·¤¯¹Ô¤ï¤ì¤ë¤³¤È¤ò¥Á¥§¥Ã¥¯¤·¡¢¤½¤Î¸åreload()¥á¥½¥Ã¥É¤Ç@book¤Î¾õÂÖ¤ò¹¹¿·¤·¤¿¤é¡¢Àµ¤·¤¯¹¹¿·¤µ¤ì¤¿¤³¤È¤ò³Îǧ¤·¤Æ¤¤¤ë

***¥ì¥³¡¼¥Éºï½ü¤Î¥Æ¥¹¥È [#wc379dfa]

test/unit/book_test.rb¤Ëtest_destroy()¥á¥½¥Ã¥É¤ò²¼µ­¤Î¤è¤¦¤ËÄêµÁ¤¹¤ë¡£

  def test_destroy
	@book.destroy
	assert_raise(ActiveRecord::RecordNotFound) { Book.find(@book.id) }
  end

¥ì¥³¡¼¥É¤òºï½ü¤·¤¿¸å¡¢¤½¤Î¥ì¥³¡¼¥É¤ò¸¡º÷¤¹¤ë¤ÈActiveRecord::RecordNotFound¤ÎException¤¬È¯À¸¤¹¤ë¤³¤È¤òassert_raise¥á¥½¥Ã¥É¤Ç¥Á¥§¥Ã¥¯¤·¤Æ¤¤¤ë

***DRY¤ÎƳÆþ [#xddd8c8d]

books.yml¤ËÄêµÁ¤·¤¿ÃÍ¡Ê´ðÁ䫤é¤Ï¤¸¤á¤ëRubyÅù¡Ë¤ò¥Æ¥¹¥È¥³¡¼¥ÉÃæ¤Ë¤Þ¤¿µ­½Ò¤·¤Æ¤·¤Þ¤¦¤Î¤ÏDRY(Don't Repeat Yourself)¤ËÈ¿¤¹¤ë¡£
¥Õ¥ì¥­¥·¥Ó¥ê¥Æ¥£¤òÊݤÁ¤Ä¤Ä¡¢¥Æ¥¹¥È¥³¡¼¥É¤ò½ñ¤¯¤Ë¤Ï²¼µ­¤Î¤è¤¦¤Ë¤¹¤ë

  def test_create
	assert_kind_of Book, @book
	# fixture̾¤ò»ØÄꤷ¤Æ¼è¤ê½Ð¤¹¤³¤È¤¬¤Ç¤­¤ë¡Ê@book¤Ç¤Ï¤Ê¤¯@books¤Ç¤¢¤ë¤³¤È¤ËÃí°Õ¡Ë
	first_book = @books["first"]
	assert_equal first_book["id"], @book.id
	assert_equal first_book["title"], @book.title
	assert_equal first_book["description"], @book.description
  end

²¼µ­¤Î¤è¤¦¤Ë¤¹¤ì¤Ð¡¢¥Þ¥Ã¥Á¤¹¤ëfixture¤ò¼«Æ°¤Çõ¤·¤ÆÃͤò¤È¤Ã¤Æ¤­¤Æ¤¯¤ì¤ë¤Î¤Ç¤è¤ê³Ú¡£(@fixture̾_¥«¥é¥à̾¡Ë


  def test_create
	assert_kind_of Book, @book
	assert_equal @first_id, @book.id
	assert_equal @first_title, @book.title
	assert_equal @first_description, @book.description
  end

¥È¥Ã¥×   ¿·µ¬ °ìÍ÷ ñ¸ì¸¡º÷ ºÇ½ª¹¹¿·   ¥Ø¥ë¥×   ºÇ½ª¹¹¿·¤ÎRSS

¥¢¡¼¥¯¥¦¥§¥Ö¤Î¥µ¡¼¥Ó¥¹¤ä¥½¥ê¥å¡¼¥·¥ç¥ó¤Ï¤³¤Á¤é