How to create a cucumber pitch definition for an editing path?

I am trying to learn how to use Cucumber and create steps using the following script (I have a model called "Vegetable" and I added a new attribute "color"):

Scenario: add color to existing vegetable When I go to the edit page for "Potato" And I fill in "Color" with "Brown" And I press "Update Vegetable Info" Then the color of "Potato" should be "Brown" 

I am currently using "learning wheels", so I have a web step (web_steps.rb):

 When /^(?:|I )go to (.+)?/ do |page_name| visit path_to(page_name) end 

Now I understand how this works with simple page navigation, such as "When I go to the vegetables home page." All I need to do is add the path to paths.rb:

 When /^the vegetable home page/ '/vegetables' 

However, in the above example, I need to go to a specific path with a specific vegetable "/ vegetables / 1" (potato url).

I'm not sure how to do this, so I tried to create my own step:

 When /I go to the edit page for "(.*)"/ do |vegetable_name| flunk "Unimplemented" end 

But I get the error:

 Ambiguous match of "I go to the edit page for "Potato"": features/step_definitions/vegetable_steps.rb:15:in `/go to the edit page for "(.*)"/' features/step_definitions/web_steps.rb:48:in `/^(?:|I )go to (.+)$/' You can run again with --guess to make Cucumber be more smart about it (Cucumber::Ambiguous) 

This is how I should do it? Or am I just using the "go to" web_steps step and somehow specify the identifier in the paths.rb file? I'm just trying to figure out how to start this after hours of reading various cucumber lessons.

+4
source share
2 answers

As DVG stated, this is because I had the appropriate step in two places where I was getting the error. To answer my own question, I can simply rely on the already provided "web_step":

 When /^(?:|I )go to (.+)?/ do |page_name| visit path_to(page_name) end 

As soon as I added the following code to paths.rb:

 def path_to(page_name) case page_name when /^the edit page for "(.*)"$/ edit_vegetable_path(Vegetable.find_by_name($1)) 

I was able to get this to work correctly.

+10
source

The problem is that you match step two places. Step definitions are global. Therefore, you need to change this function to use a step that you did not write, or to remove an extra step.

In addition, your function is written at a very low level in the ui-details way. This makes your function fragile against change, and your cucumber specification should never change unless your business requirements change. Try

 Given a vegetable named "Potato" When I mark the vegetables color as "Brown" Then the Potato should be "Brown" 

Thus, you can freely experiment with your form without changing your specification, which is the whole.

+5
source

All Articles