简体   繁体   English

如何为“编辑”路径创建Cucumber步骤定义?

[英]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"): 我正在尝试学习如何使用Cucumber并使用以下场景创建步骤(我有一个名为“Vegetable”的模型,并且我添加了一个名为“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): 我目前正在使用“训练轮”,所以我有一个网页步骤(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: 我所要做的就是添加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). 但是,通过上面的例子,我需要使用特定的蔬菜“/ 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? 或者我只是使用web_steps“转到”步骤并在paths.rb文件中以某种方式提供id? I'm just trying to figure out how to start this after hours of reading various Cucumber tutorials. 我只想弄清楚如何在阅读各种Cucumber教程后开始这个。

As DVG stated, it's because I had the matching step in two places that I was receiving the error. 正如DVG所说,这是因为我在两个地方有匹配的步骤,我收到了错误。 To answer my own question I can just lean on the "web_step" that is already provided: 要回答我自己的问题,我可以依靠已经提供的“web_step”:

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

Once I added the following code to paths.rb: 一旦我将以下代码添加到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. 我能够让它正常工作。

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. 此外,您的功能是以非常低级别的ui-details方式编写的。 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. 通过这种方式,您可以在不改变规范的情况下自由地试验表单,这就是重点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM