我目前正在开始从灯具迁移到工厂,并遇到一些测试数据库挑战。如何在每次测试之前让Rails测试数据库重建?
当我运行我的整个测试套件时,数据库被清除,并重新加载新的工厂生成的数据。但是,当我运行单个单元测试时,数据库不会清除旧值。
我可以运行rake db:test:准备在每个单独的测试之前,但这会减慢我的开发速度。
这里是我的测试设置:
self.use_transactional_fixtures = false
self.use_instantiated_fixtures = true
例如:
require File.dirname(__FILE__) + '/../test_helper'
class LocationTest < ActiveSupport::TestCase
test "should require name to save" do
location = Factory.create(:location)
end
end
将运行一次成功,但未能在随后的测试文件的运行。之前从未发生这种情况,因为测试装置会在每次运行时加载。
我已经加入厂测序,但每次运行期间,只有序列属性:
Factory.define :location do |l|
l.sequence(:name) {|n| "place#{n}"}
l.street '123 N Pitt Street'
l.state_id 4
l.city 'San Francisco'
l.location_type_id LocationType::COMMON
l.shipper_id 1
l.zip 23658
end
结果:
trunk>ruby test\unit\location_test.rb
Loaded suite test/unit/location_test
Started
.
Finished in 0.162 seconds.
1 tests, 0 assertions, 0 failures, 0 errors
>ruby test\unit\location_test.rb
Loaded suite test/unit/location_test
Started
E
Finished in 0.134 seconds.
1) Error:
test_should_require_name_to_save(LocationTest):
ActiveRecord::RecordInvalid: Validation failed: Name has already been taken
c:/ruby/lib/ruby/gems/1.8/gems/thoughtbot-factory_girl-1.2.1/lib/factory_girl/proxy/create.rb:5:in `result'
c:/ruby/lib/ruby/gems/1.8/gems/thoughtbot-factory_girl-1.2.1/lib/factory_girl/factory.rb:293:in `run'
c:/ruby/lib/ruby/gems/1.8/gems/thoughtbot-factory_girl-1.2.1/lib/factory_girl/factory.rb:237:in `create'
test/unit/location_test.rb:18:in `test_should_require_name_to_save'
1 tests, 0 assertions, 0 failures, 1 errors
地址: self.use_transactional_fixtures = true 到这个类,它的工作原理!谢谢。 是的,我无法改变整个环境的原因是有的。我甚至没有想过只为这个测试改变它...... doh! 再次感谢。 – 2009-06-03 01:19:38