- 作者: arton
- 出版社/メーカー: 翔泳社
- 発売日: 2008/04/15
- メディア: 大型本
- 購入: 6人 クリック: 113回
- この商品を含むブログ (41件) を見る
とりあえず、Scaffoldで生成されたテストを実行するとエラーになります。
% rake tenkoma@tenkoimac (in /Users/tenkoma/NetBeansProjects/memopad) /opt/local/bin/ruby -Ilib:test "/opt/local/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake/rake_test_loader.rb" "test/unit/memo_test.rb" Loaded suite /opt/local/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake/rake_test_loader Started . Finished in 0.023451 seconds. 1 tests, 1 assertions, 0 failures, 0 errors /opt/local/bin/ruby -Ilib:test "/opt/local/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake/rake_test_loader.rb" "test/functional/memos_controller_test.rb" Loaded suite /opt/local/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake/rake_test_loader Started F...... Finished in 0.131401 seconds. 1) Failure: test_should_create_memo(MemosControllerTest) [/opt/local/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/testing/core_ext/test/unit/assertions.rb:51:in `assert_difference' /opt/local/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_view/renderable.rb:83:in `each_with_index' /opt/local/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/testing/core_ext/test/unit/assertions.rb:47:in `each' /opt/local/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/testing/core_ext/test/unit/assertions.rb:47:in `each_with_index' /opt/local/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/testing/core_ext/test/unit/assertions.rb:47:in `assert_difference' ./test/functional/memos_controller_test.rb:16:in `test_should_create_memo' /opt/local/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/testing/setup_and_teardown.rb:60:in `__send__' /opt/local/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/testing/setup_and_teardown.rb:60:in `run']: <Memo.count> was the expression that failed. <3> expected but was <2>. 7 tests, 9 assertions, 1 failures, 0 errors /opt/local/bin/ruby -Ilib:test "/opt/local/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake/rake_test_loader.rb" Errors running test:functionals!
エラーになっているのは、test/functional/memos_controller_test.rbの16行目のassert_differenceの所みたいです。
test "should create memo" do assert_difference('Memo.count') do post :create, :memo => { } end assert_redirected_to memo_path(assigns(:memo)) end
ぐぐって、assert_differenceについて調べると、評価式の結果が変わることを検査するということのようです。つまり、まず、ブロックを実行する前にMemo.countの値を調べておいて、ブロックを実行したあとにまた評価して、値が変わればアサーションにパスすると。
で、なぜテストに失敗するのかというと、ここでは、「post :create...」でMemoを投稿していますが、バリデーションではじかれてるんですね。3日目の1時限目に追加したバリデーションは以下の通り。(app/models/memo.rb)
class Memo < ActiveRecord::Base validates_presence_of :text, :location validates_length_of :location, :within => 4..8 validates_exclusion_of :location, :in => %w(school street) end
なので、これに併せてテストを修正しなくてはいけません。
test "should create memo" do assert_difference('Memo.count') do post :create, :memo => {:text => 'テストデータ#3', :location => 'テストテスト'} end assert_redirected_to memo_path(assigns(:memo)) end