Thursday, December 23, 2010

Capybara labels not working

Actually, they are.

Say you have a post model, and you gave it the label foobar.
Now you try to use that label to refer to the model you've created/found.
Consider the following example:

Given a post: "foobar" exists with title: "My awesome post"
And I go to the home page
Then the post: "foobar" should exist with title: "just a post"
view raw gistfile1.rb hosted with ❤ by GitHub


This example, rather than failing by saying the model did not match the fields passed into it, violently fails by giving the error:
The model: 'the post: "foobar"' is not known in this scenario.
view raw gistfile1.rb hosted with ❤ by GitHub


Not very pretty, right?
In fact, I personally mistook this for having something wrong with my labels.
Turns out, the label is totally fine, but the error thrown, well... threw me off...

It took me way too much time to go through any bit of capybara documentation I could lay my hands on, and I still not understand why I got the error.

Finally, turns out Pickle raises this error, not because this label is not valid, but rather because the model "myteacher" doesn't match the attributes I'm requesting.

For example: the following test, which uses the same label is actually passing.
Given a teacher: "myteacher" exists with first_name: "Joe"
And I go to the home page
Then the teacher: "myteacher" should exist with first_name: "Joe"
view raw gistfile1.rb hosted with ❤ by GitHub


Hopefully this saves you some good quality time between your head and your favorite wall.
I emailed the developer of Pickle and I'm hoping he'll be kind enough to change the error a bit in future versions so that it is clearer what's happening.

Capybara and Selenium in Rails 3 tests fail and browser doesn't start

I had an issue with Firefox not starting to run Selenium in Rails 3.
Instead, my tests failed, giving me all red (even for steps that were previously passing without JS). Turns out this is caused by a dependency issue.
Someone thankfully posted a question on StackOverflow that helped me get through this.
To fix it, simply run add this to your Gemfile
gem 'database_cleaner'
view raw gistfile1.rb hosted with ❤ by GitHub

Then run
bundle install
view raw gistfile1.rb hosted with ❤ by GitHub

This should fix it for you.

You can also track this issue at Github, where I posted this very solution.