Factory Girl created objects that do not clean between tests?

I have difficulty implementing tests with a little confusion. With User.create, I can create and save in several tests:

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

but using Factory.create, it throws an ActiveRecord duplicate write error:

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

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

Error: "ActiveRecord :: StatementInvalid: Mysql :: Error: duplicate entry"

What gives?

+3
source share
1 answer

You have the following line in spec_helper:

config.use_transactional_fixtures = true

This tells rspec / test :: unit to start the transaction from the very beginning of each test case and issue a rollback when it completes.

+1
source

Source: https://habr.com/ru/post/1759465/


All Articles