简体   繁体   English

使用web_steps.rb进行黄瓜声明式步骤定义

[英]cucumber declarative step definitions using web_steps.rb

When using Cucumber for BDD (CS169.x1@edX) the following message is returned in response to execution of 'cucumber features/filter_movie_list.feature': 当将Cucumber用于BDD(CS169.x1@edX)时,响应于执行“ cucumber features / filter_movie_list.feature”,将返回以下消息:

When I uncheck the following ratings: G, PG-13                 # features/step_definitions/movie_steps.rb:44
  Undefined step: "When I uncheck "ratings_G"" (Cucumber::Undefined)
  ./features/step_definitions/movie_steps.rb:52:in `block (2 levels) in <top (required)>'
  ./features/step_definitions/movie_steps.rb:49:in `each'
  ./features/step_definitions/movie_steps.rb:49:in `/I (un)?check the following ratings: (.*)/'
  features/filter_movie_list.feature:30:in `When I uncheck the following ratings: G, PG-13'
...
You can implement step definitions for undefined steps with these snippets:

When /^When I uncheck "(.*?)"$/ do |arg1|
  pending # express the regexp above with the code you wish you had
end

The particular configuration in question has a declarative step in 'features/filter_movie_list.feature' that says: 有问题的特定配置在'features / filter_movie_list.feature'中有一个声明性步骤,内容为:

When I uncheck the following ratings: G, PG-13 当我取消选中以下评分时:G,PG-13

an attempt to implement the declarative steps in 'features/step_definitions/movie_steps.rb' (to reuse the imperative steps of 'features/step_definitions/web_steps.rb') which looks like: 尝试在“功能/step_definitions/movie_steps.rb”中实现声明性步骤(以重用“功能/step_definitions/web_steps.rb”的命令性步骤),如下所示:

When /I (un)?check the following ratings: (.*)/ do |uncheck, rating_list|
  step "When I uncheck \"ratings_G\""

and a working 'features/step_definitions/web_steps.rb' file (ie the one created by 'rails generate cucumber_rails_training_wheels:install' after gem installation) with an imperative default step like: 和一个有效的'features / step_definitions / web_steps.rb'文件(即在安装gem后由'rails generatecumulage_rails_training_wheels:install'创建的文件)具有强制性的默认步骤,例如:

When /^(?:|I )uncheck "([^"]*)"$/ do |field|
  check(field)
end

Also, it seems like 'features/step_definitions/web_steps.rb' is accessible to Cucumber since adding When I uncheck "ratings_G" to 'features/step_definitions/movie_steps.rb' succeeds; 另外,由于向“功能/step_definitions/movie_steps.rb”添加When I uncheck "ratings_G"成功,因此黄瓜似乎可以访问“ features / step_definitions / web_steps.rb”。 anyone have any idea of what might be causing such behavior? 任何人都不知道可能会导致这种行为的原因吗? The implementation of the declarative step has even been simplified so that no variable substitution takes place... 声明性步骤的实现甚至已经简化,因此不会发生任何变量替换。

I've tried: 我试过了:

When I uncheck the following ratings: "G, PG-13"

Then in the steps file: 然后在步骤文件中:

When /^I uncheck the following ratings: "([^"]*)"$/ do |arg1|
  puts "arg1: #{arg1}"
end

.. seems to work for me. ..似乎为我工作。

Problem 问题

The problem is with how you attempt to call a step from another step. 问题在于您如何尝试从另一个步骤调用一个步骤。

The second line of the code: 代码的第二行:

When /I (un)?check the following ratings: (.*)/ do |uncheck, rating_list|
  step "When I uncheck \"ratings_G\""

is looking for a step definition like: 正在寻找类似的步骤定义:

When /^When (?:|I )uncheck "([^"]*)"$/ do |field|
  check(field)
end

Notice that it is looking for "When" inside the step regex. 注意,它正在步骤正则表达式中寻找“何时”。 This unlikely exists, hence the undefined step error. 这种可能性不大,因此存在未定义的步进错误。

Solution

To call the step from a step you need to do one of the following: 要从某个步骤调用该步骤,您需要执行以下操作之一:

1) Use step and make sure not to include the Given/When/Then keyword: 1)使用step并确保不包括Given / When / Then关键字:

When /I (un)?check the following ratings: (.*)/ do |uncheck, rating_list|
  step "I uncheck \"ratings_G\""

2) Or if you prefer to include Given/When/Then, use steps instead: 2)或者,如果您希望添加“给定/何时/然后”,请改用以下steps

When /I (un)?check the following ratings: (.*)/ do |uncheck, rating_list|
  steps %Q{
    When I uncheck "ratings_G"
  }

See the Cucumber wiki . 请参阅黄瓜Wiki

In this case the course material which was provided illucidates the difference but did not lead us to delineate between two steps that are very similar; 在这种情况下,所提供的课程资料说明了差异,但并未使我们在非常相似的两个步骤之间进行划分; changing "When I uncheck the following..." to "When unchecking the following..." allows web_steps.rb to come back into scope and get executed as desired (the previously linked posting, link: cucumber contradictory error messages , has more details). 将“当我取消选中以下项...时”更改为“当取消选中以下项...时”可以使web_steps.rb返回范围并根据需要执行(先前链接的发布, 链接:黄瓜矛盾的错误消息 ,还有更多内容)细节)。

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

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