I’m not sure what’s going on, but I noticed that everytime I run my test with:
bundle exec rspec
the data in my development database disappears
I guess it has something to do with the DatabaseCleaner gem I installed, but I’m not sure what. I just copied the configuration lines from my previous Rails app and copied it into spec/spec_helper.rb:
config.order = "random"
config.before(:suite) do
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do
DatabaseCleaner.strategy = :transaction
end
config.before(:each, :js => true) do
DatabaseCleaner.strategy = :truncation
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
This is exactly what database cleaner is for, so it’s working as expected. The idea is that you need to build up the context you need in the database for a given test (and only that test) to pass before that test is run. That way every test can be run in isolation or in a random order, etc. And after every test the database will be in a clean state again so you can build up a new set of data to pass the next test.
It sounds like you’re currently relying on sharing state between tests, which is almost always a bad idea. But, if you absolutely need certain data seeded before every test (maybe you’re building an olympic score keeping app and you have a countries table that you’ll seed in production with all the countries participating or something), then you can configure rspec and/or cucumber to run your db:seed task before each run. Warning: this could slow down your test suite depending on how much data you need to pump in before a test load, but on the other hand if it’s data that won’t change (lookup tables, essentially) and you seed the db once before the whole test run instead of inserting and deleting that list of countries 200 times over the course of the whole test suite, then it can be advantageous