Troubleshooting Feature Specs

or how I learned about Database Cleaner

Closeup Brooms by OpenRoadPR is licensed under the Creative Commons Zero Licence

Adding the first couple of feature tests to a codebase can be tricky. You often know what you want to test and how you want to test it, but keep running into problems because you need to complete all of the setup.

While recently working in a new codebase, adding our first feature tests, I kept getting a failing test. The failure stated Capybara::ElementNotFound: Unable to find option "option 1". In our case, that meant the test couldn’t select an option from a dropdown. I spent some time investigating with save_and_open_page as well as save_and_open_screenshot and found that the dropdown was being created, but there were no options in it.

To troubleshoot this, I:

  • Checked factories
  • Checked test setup
  • Made sure the test knew that there was JavaScript that needed to be executed inside
  • Messed with drivers
  • Googled all sorts of word concoctions related to the idea of “dropdown options not appearing in feature test”.

After triple checking the test setup and making sure it knew to run the JavaScript in the test, I learned about the transactional database cleaner. I was running the test in a transaction. This means that the record was being created, but not getting to the separate Poltergeist server.

Now, for most tests, running them transactionally is preferred. After the test runs there will be a transactional clearing, or in other words, it deletes the records. This, however, is not what I want for tests that involve JavaScript. For that, I needed the record to be both in the test AND the Poltergeist server. To do that, I needed Database Cleaner. Database Cleaner is pretty straightforward to set up and the docs are quite helpful. In this scenario, the main purpose of Database Cleaner is to use truncation instead of transaction for tests requiring JavaScript execution and this is just one way Database Cleaner can be useful in testing. Once Database Cleaner was in place, the tests ran like a dream!

Photo of Allison McMillan

Allison was first introduced to programming at a Rails Girls workshop after a career as a nonprofit executive. She is also an international conference speaker living in the Washington, DC area. When she’s not writing code for us, she invests her time leading the People Committee which focuses on the health and happiness of our team members!

Comments