views:

24

answers:

1

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?

A: 

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.

nathanvda
Worth checking also that your test database supports transactions. For example, mysql using the default MyISAM storage engine does not.
Shadwell