Paths in features

October 16, 2008

Been thinking about specifying paths in stories and steps, and the brittleness they can introduce to tests. This was prompted by a bug in rspecs ‘render_template’ matcher.

First of all the bug …

This is rails testcode that works ok

assert_template(“/”)

This is the rspec equivalent

response.should render_template("/")

which throws the exception

wrong argument type nil (expected Regexp) (TypeError)  /Library/Ruby/Gems/1.8/gems/rspec-rails-1.1.8/lib/spec/rails/matchers/render_template.rb:22:in `match'
 /Library/Ruby/Gems/1.8/gems/rspec-rails-1.1.8/lib/spec/rails/matchers/render_template.rb:22:in `matches?'

Paths Don’t Belong In Stories

Feeling more convinced about this the more I think about it. Basically a specific path is an implementation detail which can change depending (obviously) on how you implement. So if you want to view some products your story might be something like

When I click products
Then I should see products

This is more robust than

When I click products
Then I should go to /products

In a similar way when writing the steps you should use the products_path method rather than using ‘/products’