2010-08-13 70 views
3

我被困在实施测试时略有混淆。随着User.create我可以创建并保存在多个测试:Factory Girl创建的对象在测试之间没有清除?

should "test something" do 
    u1 = User.create(:first_name => "Fred", :last_name => "Flintstone") 
    assert true 
end 

should "test something else" do 
    u1 = User.create(:first_name => "Fred", :last_name => "Flintstone") 
    assert true 
end 

但使用Factory.create,它抛出一个ActiveRecord的重复输入错误:

should "test something" do 
    Factory.create(:amanda_levy) 
    assert true 
end 

should "test something else" do 
    Factory.create(:amanda_levy) 
    assert true 
end 

错误:“的ActiveRecord :: StatementInvalid:Mysql的::错误:重复项“

什么给?

+0

什么是:amanda_levy,这可能是问题的来源 – s84 2010-08-13 06:22:41

+0

这只是Factory.create的工厂女孩​​语法(:factory_name) – ambertch 2010-08-16 02:38:47

回答

1

你有没有下面一行在你spec_helper:

config.use_transactional_fixtures = true 

这告诉RSpec的/测试::单位在每个测试用例的开始 启动一个事务,并发出回滚时,它已经结束了。

+1

值得检查您的测试数据库是否支持事务。例如,使用默认MyISAM存储引擎的mysql不会。 – Shadwell 2010-08-13 10:08:04

相关问题