2010-03-17 6 views
2

Étant donné les éléments suivants dans Concombre:concombre enregistrement ID

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". 

Comment puis-je savoir que ID?

Merci!

Répondre

2

Départ: http://railscasts.com/episodes/186-pickle-with-cucumber

En regard particulier l'exemple de conserves au vinaigre où il crée un produit:

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" 

Notez comment il fait référence au produit créé comme ce produit. Pickle s'en chargera pour vous.

Bonne chance.

+0

Et comment cela se ferait sans conserves au vinaigre? –

+0

Dans ce cas, j'écrirais une étape personnalisée pour trouver une voiture donnée par son nom, puis j'utiliserais shoulda pour déterminer s'il s'agit d'une correspondance. – Jonathan

3

Vous pouvez l'écrire comme:

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" 

La définition de la dernière étape pourrait être:

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 
+2

D'accord que c'est la meilleure approche - les étapes peuvent s'appuyer sur les choses qui sont venues avant, mais pas sur ce qui vient après. Vous pouvez le rendre encore plus simple en définissant une variable d'instance sur l'étape _Given_. (Par exemple, '@car = [[quelque chose]]', puis reportez-vous à '@ car' plus loin dans vos instructions _Then_ pour ne plus avoir à le retrouver.) – SFEley