ambertch
ambertch

Reputation: 7649

Factory Girl created objects not clearing in between tests?

I'm being held up implementing tests with a slight confusion. With User.create I can create and save in multiple 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 a ActiveRecord duplicate entry 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?

Upvotes: 3

Views: 577

Answers (1)

nathanvda
nathanvda

Reputation: 50057

Do you have the the following line in your spec_helper:

config.use_transactional_fixtures = true

That tells rspec/ test::unit to start a transaction at the start of every test case, and issue a rollback when it's over.

Upvotes: 1

Related Questions