views:

240

answers:

3

Similar to the problem described here: http://rpheath.com/posts/411-how-to-use-factory-girl-with-rspec

in Short (shorten'd code):

spec_helper:

config.use_transactional_fixtures = true
config.use_instantiated_fixtures  = false

factories.rb:

Factory.define :state do
  f.name "NY"
end

in my spec

before(:each) do 
  @static_model = Factory(:state) # with validate uniqueness of state name
end

error:

duplicate entry name "NY" etc.


Question: Shouldn't rspec clear database before each spec example and hence not throwing duplicate entry errors?

A: 

Things i think off:

  • do you use rake spec to run your testsuite: that builds up the database from scratch (to make sure nothing is sticking)
  • do you use, anywhere, a before (:all) ? Because whatever you create inside a before :all should be deleted again in a after :all or it keeps on existing.
nathanvda
A: 

Some more possible causes:

  • There's still a states.yml fixture sitting around
  • Someone played around on script/console test and forgot to clean up afterwards.
henning-koch
+1  A: 

You might also find it's because you haven't wrapped the statement in:

describe "what it should do" do
  @static_model = Factory(:state) # with validate uniqueness of state name
end

I discovered that was the change that solved this problem: http://stackoverflow.com/questions/3497135/why-isnt-factory-girl-operating-transactionally-for-me-rows-remain-in-databas/3498959#3498959

Peter Nixey