factory_girl + rspec doesn't seem to roll back changes after each example

Rex picture Rex · Jul 26, 2010 · Viewed 11.7k times · Source

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?

Answer

nathanvda picture nathanvda · Jul 26, 2010

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.