I think it is important to actually understand the code that I copy/paste, so I took a little time to read up on the details which I’ve summarized below.
Favorite Testing Gems
I won’t elaborate on RSpec, the concepts in this post likely applies to test-unit as well. I written before about why RSpec is my favorite.
Capybara is the world’s largest rodent, and the Ruby community is also the name of a favorite gem for testing the content of web pages. (A successor to Bryan Helnkamp‘s WebRAT, so named for Ruby Acceptance Testing, which instigated the rodent naming theme) Capybara is wonderful with its support for many “drivers” which allow for a consistent API across different solutions that offer different levels of browser support, with different performance characteristics.
One of its creators, Joe Ferris, explains how it works (via stackoverflow)
- Capybara boots up your rack application using webrick or thin in a background thread.
- The main thread sets up the driver, providing the port the rack application is running on.
- Your tests ask the driver to interact with the application, which causes the fake web browser to perform requests against your application.
The DatabaseCleaner gem is super helpful for our typical Rails app that relies on a database. We always want a “clean slate” when we start our tests and this nifty gem gives us a bunch of options with a consistent interface for various database choices.
To configure these solutions correctly, it is critical to understand that with Capybara::Webkit our target app code is running in a separate process from our tests. This means that when we set up our test data RSpec is running in one process and needs to actually write to the database, then our app code reads from the database from another process. Wheras with Rack::Test, the tests and the target code runs in the same process. That’s why we can’t use a “transaction” strategy to reset our test environment with Capybara::Webkit. Instead we use the “truncation” strategy, which simple blows away all of the data after each test run.
Why bother with transactions?
Truncation will work just as well with Rack::Test as transcations, so why introduce the complexity of two different configurations? The Database Cleaner README explains: “For the SQL libraries the fastest option will be to use :transaction as transactions are simply rolled back.” Sarah Mei elaborated on this by reminding me that the commit to the database is what takes the most time, and the transaction is never committed, it is simply rolled back at the end of your test. Transactions are pretty speedy, so we want to only use the truncation method when absolutely necessary.
Just Show me the Code
Here’s the configuration that was documented by Eric Saxby from Wanelo, which worked for me as well:
config.use_transactional_fixtures = true
config.before(:each, js: true) do
self.use_transactional_fixtures = false
DatabaseCleaner.strategy = :truncation
config.after(:each, js: true) do
self.use_transactional_fixtures = true
How does this work exactly?
We are set up to use transactions by default, which is built into rspec-rails and does not rely on DatabaseCleaner. Then, for our JS tests, we tell RSpec not to use transactions and instead instruct DatabaseCleaner to set up before each test runs with
DatabaseCleaner.start and then clean up after with
I have no idea why
ActiveRecord::Base.establish_connection is needed, but if we don’t do that, then
rake spec hangs after my first JS test with this ominous warning:
WARNING: there is already a transaction in progress
Perhaps someone reading this can explain this detail, but happy to have a configuration that works and hope this helps other folks who want fast tests that run reliably.