R – Cucumber and webrat – How to handle dynamic URLs in the paths.rb

cucumberruby-on-railswebrat

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?

Best Answer

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)
Related Topic