[rspec-users] Then I should be on /users/3/posts/8/comments/2/edit

Ben Mabey ben at benmabey.com
Fri Apr 10 00:51:37 EDT 2009

Gavin Hughes wrote:
> "Then I should be on /users/3/posts/8/comments/2/edit"
> What's the solution for parsing out and matching and arbitrarily deep
> nested route?
Hi Gavin,
Let me try to answer your question without actually answering it. :)

I generally don't test my URLs.  IMO, for the majority of cases the URL 
is merely an implementation detail of the application.  For example I 
could easily see the URL in your example 
(/users/3/posts/8/comments/2/edit) as being "/posts/8/comments/2/edit" 
or "/comments/2/edit" or "/posts/the-name/comments/2/edit".  The user 
would be fine with all of these cases, they really don't care one way or 
the other.  Instead of focusing on an implementation detail of the 
application scenarios should be focused on the behaviour that the user 
would like to see.  In your particular case what the user really cares 
about is being able to edit a comment, correct?  So, instead of just 
verifying that the user is on the right page after clicking on "Edit 
Comment", you should have the user actually fill out the form to edit 
the comment.  Then verify that the comment is actually updated on the 
post's page after they submit the comment editing form.  Off the top of 
my head something like this may work:

  Scenario: edit comment
    Given a post exists named "big news"
    And I have made a comment "bad post!" on the "big news" post
    And I am viewing the "big news" post

    When I change my "bad post!" comment to "Great post!"
    And press "Update Comment"

    Then I should see "Great Post!"

With this you are just testing the behaviour of the application. (BTW, 
you could write the scenario above number of ways and still be testing 
the same thing- I'm not sure if I love the way I wrote it....) You could 
change your URL naming scheme and this would still pass because the 
behaviour hasn't changed.  By embedding the URL in the scenario your URL 
scheme is now coupled to your features unnecessarily.

Sorry, for not really answering your question but I think you will thank 
me later if you take this approach. :)


More information about the rspec-users mailing list