views:

537

answers:

2

I am using Cucumber for BDD development in my Ruby on Rails project and I'm running into some confusion on how the path.rb handles paths used in rails applications.

Given I have:

class Parent < ActiveRecord::Base
  has_many :children
end

class Child < ActiveRecord::Base
  belongs_to :parent
end

and I have the following Cucumber feature:

Scenario: A test feature
    Given I am on the parent page
     When I follow "Link to Children"
     Then I should be on the children list page

with the path defined as:

def path_to(page_name)
  case page_name
  when /the children list page/
       '/parents/:id/children'
end

The problem I come across is the following error when running the feature:

Spec::Expectations::ExpectationNotMetError: expected: "/parents/:id/children",
 got: "/parents/1726/children" (using ==)

I don't really care what the :id is. What should I do instead? Is this even possible with the default web steps? Am I thinking about the problem in the wrong way?

+3  A: 

The way I do it, which may not be the best way is as follows:

when /the children list page for "(.+)"/
    p = Parent.find_by_name($1)
    parent_children_path(p)
jonnii
+1  A: 

In our app, we always wanted a new record in the database whenever a user clicked the "new" button. Thus, our controller's new action automatically calls create and then redirects to the edit action.

We faced a similar problem in testing, when we didn't care so much about what the ID was -- just that it got to the edit page for the app.

Here's what I came up with.

(NOTE: The step definition is written using capybara, but it shouldn't be too different from webrat)

Then /^(?:|I )should now be editing the (.*)$/ do |model|
  id = find_by_id("#{model}_id").value
  Then "I should be on the edit #{model} page for \"#{id}\""
end

The basic premise is that when you're on a Rails edit page, there will be a form for the model you're editing. That form always contains a hidden field with the ID of the specific record you're editing.

The step finds the hidden field, extracts the ID from it, and then looks for a web_step to resolve the path for that model.

Just make sure you have a path that matches for the model you're looking up.

when /the edit person page for "([^\"]*)"/
  edit_person_path($1)
Chris