3

我在实施测试时感到有些困惑。使用 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”

是什么赋予了?

4

1 回答 1

1

您的 spec_helper 中是否有以下行:

config.use_transactional_fixtures = true

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

于 2010-08-13T06:23:05.673 回答