Last week my pair and I ran into a problem with a failing Cucumber scenario. We were using one of the more awesome features of Cucumber, multiline tables, when we got the unexpected failure. We realized that what we were doing was probably a common pattern and would certain trip other’s up.
Scenario and Step
If you’re not familiar with multiline tables in cucumber, take a look at the wiki entry. We were specifying a list of attributes on one of our models that we wanted to have access to in our step. First, here is an example scenario outline.
1 2 3 4 5 6 7 | Scenario: Finding a specific dog Given an existing microchipped dog named "George" When I search for a dog with the following attributes: | name | microchipped | | George | true | Then I find "1" dog And that dog should be named "George" |
Our step was taking the multiline table attributes and using them in an ActiveRecord find, like this.
1 2 3 4 5 | /^I search for a dog with the following attributes:$/ do |table| table.hashes.each do |attributes| @dog = Dog.find(:first, :conditions => attributes) end end |
In our example we are looking for two attributes a string called “name” and a boolean called “microchipped”. However, this find will always fail, even if your ‘Given an existing microchipped dog named “George”‘ step explicitly sets up the correct object in the beginning of the test.
The Fix
We found that if we changed our scenario to look like this, everything worked fine.
1 2 3 4 5 6 7 | Scenario: Finding a specific dog Given an existing microchipped dog named "George" When I search for a dog with the following attributes: | name | microchipped | | George | 1 | Then I find "1" dog And that dog should be named "George" |
See the change? We changed “true” for microchipped to “1″. Typically when you’re working with rails you can pass true in the conditions of an ActiveRecord find and Rails will convert that to the correct SQL string for you. However, because cucumber passes each argument in the multiline table rows as strings, Rails never has the chance to covert that true for you.
In the first, failing, example you end up with something like this for your SQL:
SELECT * FROM dogs WHERE name = "George" AND microchipped = "true" LIMIT 1;
In the second, passing, example you end up with something like this:
SELECT * FROM dogs WHERE name = "George" AND microchipped = "1" LIMIT 1;