Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Factory Girl创建的对象在测试之间未清除?_Ruby On Rails_Factory Bot - Fatal编程技术网

Ruby on rails Factory Girl创建的对象在测试之间未清除?

Ruby on rails Factory Girl创建的对象在测试之间未清除?,ruby-on-rails,factory-bot,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_nam

我在执行测试时有点困惑。使用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::错误:重复条目”


提供了什么?

您的spec\u助手中是否有以下行:

config.use_transactional_fixtures = true
它告诉rspec/test::unit在开始时启动事务
创建(:Factory_name)并在测试结束后发出回滚。

什么是:amanda_levy,这可能是问题的根源,这只是Factory的Factory Girl语法。创建(:Factory_name)也值得检查您的测试数据库是否支持事务。例如,使用默认MyISAM存储引擎的mysql不支持。