Cucumber tests failing – looked like it was using a different database

Just spent the morning tracking down an issue where a number of our cucumber tests were failing to find the database in the state that I had just set up with my step definitions.

For example, I had a step definition:

Given /I have (\d+) servers$/ do |num_servers|
  num_servers.to_i.times {Factory.create(:server)}

When the browser visited the page after this code had run, it still saw the “server” records from the fixtures instead of the factory generated record.  I checked the database connections, and even put debugging code into the step above to confirm the records that were in the database.  The debugging output matched the step definition, but the browser didn’t.  I confirmed visually by using the Selenium WebDriver that it was still seeing fixture records.

I bit of googling and I found a page discussing Browsers and Transactions – I remember reading and fixing this once before, but a quick check of the features/support/env.rb file showed that my “fix” had been changed.

The line:

DatabaseCleaner.strategy = :transaction

won’t work as any change to the database done inside a step will not be seen by the web browser.  A quick change, resetting this back to:

DatabaseCleaner.strategy = nil

and all tests working again.

Of course, this all would have much easier to track if this company had automated testing and a decent version control system, but that’s a topic for another post.

Leave a Comment

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>