April 28th, 2010
A couple of snags I stumbled upon today, which may be handy to someone, relate to rack-test, sessions, and http request methods.
For a more comprehensive introduction see Tim Riley's post.
First of all take the following example
Given I am logged in as an admin When I go to "bulletin/volumes/" And I follow "New Article"
This worked fine with webrat, but with capybara (and rack-test) as soon as the page changed the user lost their logged-in-ness, no log-out being performed, the session was just being lost by the testing framework.
While flailing around for a solution, or even better for the problem, I noted that named paths worked just fine. The culprit, it turns out, is the lack of a leading /. Laugh, cry, or curse profusely.
Given I am logged in as an admin When I go to "/bulletin/volumes/" And I follow "New Article"
Also on the authentication features but a somewhat different topic is logging out. Log out links are popular, and with a restful setup this often means using :method => :delete.
Capybara doesn't provide put and delete HTTP methods as part of its top level api; it is driver agnostic, and when using drivers backed by a web browser (selenium, for example) those browsers can't send put and delete requests (it's not a Rails testing framework, so faking it isn't applicable).
Still, rack-test does provide these methods, so if you don't mind your step being driver-specific (and thanks to tagging you shouldn't) you can do something like the following:
When /^I HTTP delete to the session path$/ do rack_test_session_wrapper = Capybara.current_session.driver rack_test_session_wrapper.process :delete, admin_session_path #[capybara](http://github.com/jnicklas/capybara) wrapper which will pass along the current rack env end
#process is the important bit, if you just use delete path in your stepdef you'll get a delete request to the correct url, but it won't send the current rack environment so you'll lose your session, which in my case meant the user was not logged out, since the app didn't know who it was supposed to be logging out.
Now, on to those other broken stepdefs!