Railsのテストでエラー
RailsによるアジャイルWebアプリケーション開発の中のモデルのテストのところで
def test_destroy @product.destroy assert_raise(ActiveRecord::RecordNotFound) { Product.find(@product.id) } end
を実行するところでエラーになった。
The problem is that this test fails with following error: Loaded suite test/unit/product_test Started ..E Finished in 0.078 seconds. 1) Error: test_update(ProductTest): ActiveRecord::RecordNotFound: Couldn't find Product with ID=1 c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/base.rb:412:in `find' test/unit/product_test.rb:7:in `setup_without_fixtures' c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/fixtures.rb:523:in `setup' c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/fixtures.rb:521:in `setup' 3 tests, 8 assertions, 0 failures, 1 errors
ググッて見ると MySQL で MyISAM を使ってることが原因との事。
Google Groups - Data in test database doesn't refresh
クラスの始めに以下の設定を記述したら無事テストが通るようになった。
self.use_transactional_fixtures = false
他の全てのテストに同じ事を書くのは Rails の信条に反するので test_helper.rb に書く方がいいんじゃないかと思って test_helper.rb を開くと self.use_transactional_fixtures が true で設定してあった。ここを false に変更し、クラスへの記述を除去。