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:
c
Some more possible causes:
Question: Shouldn't rspec clear database before each spec example and hence not throwing duplicate entry errors?
RSpec with DatabaseCleaner or RSpec Rails with use_transactional_fixtures will clear the DB as long as your created the data in the example itself. before :all do ... end
is considered outside of the example, because the data remains untouched across multiple examples. Whatever you create in before :all
you have to delete in after :all
.
In order to delete whatever you create automatically use before :each do ... end
. Be aware the same data will be created and removed 10 times if you have 10 examples. The difference between before :all
and before :each
is better explained here: rails rspec before all vs before each
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: Why isn't factory_girl operating transactionally for me? - rows remain in database after tests
When you use Factory(:state) wich is a shortcut to Factory.create(:state), factory_girl returns you a saved object.
Use Factory.build(:state) instead.
I have had similar questions about what sort of starting state one can expect when using FG and RSpec.
While I too wait for clarity, Database Cleaner could be a good fix: http://rubydoc.info/gems/database_cleaner/0.6.7/frames hth - Perry
Things i think off:
rake spec
to run your testsuite: that builds up the database from scratch (to make sure nothing is sticking)before (:all)
? Because whatever you create inside a before :all
should be deleted again in a after :all
or it keeps on existing.