Webrat Visits and Redirects

October 15, 2008

Depressing how long it took me to find out that the visits method in webrat follows all redirects. This means that having a story like

When  I visit /products/1
Then  I should be redirected to the home page

is nonsense as you cannot detect the redirect. In fact all webrats methods follow redirects

Of course you can detect that you have gone to the home page so

When  I visit /products/1
Then  I should go to the home page

Another alternative is to change the implementation of the redirect step. Currently restful authentication has

Then "$actor should be redirected to $path" do |_, path|
  response.should redirect_to(grok_path(path))
end  

We could change our story implementation to something like

Then "$actor should be redirected to $path" do |_, path|
  response.url should_be (grok_path(path))
end  

However this isn’t a great solution either.

Webrat isn’t to blame here. It has the strong opinion that how you get to a particular location is an implementation detail, and that such detail has no place in features stories or integration tests which is what webrat targets. Restful_Authentication is making the mistake of introducing this detail into its story testing and compounding this by using webrat to implement many of its tests. This has the potential to cause all sorts of problems

I agree with webrat that the mechanism of redirecting is the sort of detail should be tested at a lower level in functional/controller tests, and because the word redirect is so closely associated with the mechanism of redirection - it has no place in our features.

So our original story should become

When  I visit /products/1
Then  I should go to the home page

An additional benefit with this clearer language is that it makes it significantly clearer how wrong our example story is