Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a Cucumber step definition for a "edit" path?

I'm attempting to learn how to use Cucumber and creating steps using the following scenario (I've got a model called "Vegetable," and I've added a new attribute called "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'm currently using "training-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 a simple page navigation such as "When I go to the vegetable home page." All I have to do is add a path to paths.rb:

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

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

I'm not sure how to do this, so I tried created 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)

Is this how I'm supposed to do this? Or do I just use the web_steps "go to" step and provide the id somehow in the paths.rb file? I'm just trying to figure out how to start this after hours of reading various Cucumber tutorials.

like image 983
5StringRyan Avatar asked Dec 06 '22 13:12

5StringRyan


2 Answers

As DVG stated, it's because I had the matching step in two places that I was receiving the error. To answer my own question I can just lean on the "web_step" that is already provided:

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

Once 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 properly.

like image 61
5StringRyan Avatar answered Dec 29 '22 13:12

5StringRyan


The problem is that you are matching the step two places. Step definitions are global. So you have to change the feature to use a step you don't have written two places or delete the redundant step.

Also, your feature is written in a very low level has-ui-details way. This makes your feature brittle against change, and your cucumber spec should never change unless your business requirements change. Consider trying

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

This way you can freely experiment with your form without altering your specification, which is the whole point.

like image 23
DVG Avatar answered Dec 29 '22 13:12

DVG