I’m using Capybara through Cucumber to test a Rails app.
The Rails app has some seed data in db/seeds.rb, which sets up the authorisation roles and a default user account, and I’ve added a test_seed.rake file to seed the test database after rake db:test:prepare is run, as rake db:seed doesn’t seed the test database.
When I run the Cucumber features using Capybara, this seed data is left alone, and any changes made in testing each feature are rolled back.
However, if I try to test an AJAX-based feature, by putting @javascript in front of the test so Selenium will run it, the seed data is erased once the test is complete. This means subsequent authorisation-based tests fail until rake db:test:prepare is run again, as they are unable to find a role_id when creating user accounts.
Why is Selenium doing this? More to the point, how can I stop it?
(I know I could use Cucumber hooks to load the data before each test. But this data also needs to be loaded before the RSpec-based unit tests. It also needs to be loaded into the development and production databases. So using the built-in seed mechanism seems to be more DRY. In any case, the database shouldn’t be rolled back differently just because Selenium got invoked!)
Edit: I’m using database_cleaner with the :transaction strategy. The docs at https://github.com/jnicklas/capybara mention database_cleaner, but seem to only be talking along the lines of using it with :truncation to make everything else work the same way as Capybara does things when invoking external browsers. I’ve not tried the patch on that page as I don’t fancy “strange failures” — is there a cleaner way? I’m beginning to think removing the tests and just eyeballing the AJAX stuff would be the quickest, easiest way…
I ended up changing the DatabaseCleaner.strategy to
:truncationfor both Cucumber and RSpec, and moving to SeedFu to seed the database.For Cucumber, add this to
env.rb:(remember to change the DatabaseCleaner.strategy!)
For RSpec, add this to
spec_helper.rb:Sadly, this makes all tests run a little bit slower, as previously the database was only seeded once and then rolled back, as opposed to being seeded each time. Even with only five roles in the seed file, it’s noticeable.