views:

114

answers:

2
+2  Q: 

Cucumber Record ID

Given the following in Cucumber:

Given a car exists with title: "Toyota"
And I go to path "/cars"
And I follow "Toyota Page"
And I should be on path "/cars/CAR_ID"
Where CAR_ID is the ID of the car titled "Toyota".

How do I figure out that ID?

Thanks!

+1  A: 

Check out: http://railscasts.com/episodes/186-pickle-with-cucumber

In particular look at the pickle example where he creates a product:

Scenario: Show product
  Given a product exists with name: "Milk", price: "2.99"
  When I go to the show page for that product
  Then I should see "Milk" within "h1"
  And I should see "$2.99"

Note how he refers to the created product as that product. Pickle will take care of that for you.

Good luck.

Jonathan
And how would that be done without pickle?
yuval
In that case I would write a custom step to find a given car by name, then use shoulda to determine if it is a match.
Jonathan
+2  A: 

You could write it like:

Given a car exists with title: "Toyota"
When I go to path "/cars"
And I follow "Toyota Page"
Then I should be on the page for the car: "Toyota"

The definition for the last step could be:

Then /^I should be on the page for the car: "([^\"]*)"$/ do |car_title|
  car = Car.find_by_title(car_title)
  assert_equal "/cars/#{car.id}", URI.parse(current_url).path
end
jrallison
Agreed that this is the better approach -- steps can rely on the things that came before, but not on what comes after. You could make it even simpler by setting an instance variable on the _Given_ step. (E.g., `@car = [[something]]` and then refer to `@car` later on in your _Then_ statements so you don't have to go finding it again.)
SFEley