Factory Girl在测试之间创建了不清除的对象?

时间:2010-08-13 05:54:50

标签: ruby-on-rails factory-bot

我被阻止实施测试,但有点混乱。使用User.create,我可以创建并保存多个测试:

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 :: Error:Duplicate entry”

是什么给出了?

1 个答案:

答案 0 :(得分:1)

您的spec_helper中是否有以下行:

config.use_transactional_fixtures = true

告诉rspec / test :: unit在开始时启动一个事务 每个测试用例,并在结束时发出回滚。