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

时间:2012-11-23 08:05:15

标签: ruby-on-rails ruby cucumber

使用Cucumber for 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

有问题的特定配置在'features / filter_movie_list.feature'中有一个声明性步骤,其中包含:

  

当我取消选中以下评级时:G,PG-13

尝试实现'features / step_definitions / movie_steps.rb'中的声明性步骤(以重用'features / step_definitions / web_steps.rb'的命令性步骤),如下所示:

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

和一个正在运行的'features / step_definitions / web_steps.rb'文件(即'rails生成gem_rails_training_wheels:install'创建宝石后创建的文件),其命令默认步骤如下:

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

此外,似乎'features / step_definitions / web_steps.rb'可供黄瓜访问,因为将When I uncheck "ratings_G"添加到'features / step_definitions / movie_steps.rb'成功;任何人都知道可能导致这种行为的原因是什么?声明步骤的实现甚至已经简化,因此不会发生变量替换......

3 个答案:

答案 0 :(得分:0)

我试过了:

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

然后在步骤文件中:

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

..似乎对我有用。

答案 1 :(得分:0)

<强>问题

问题在于您如何尝试从另一个步骤调用步骤。

代码的第二行:

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

正在寻找一个步骤定义,如:

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

请注意,它正在寻找步骤正则表达式中的“When”。这不太可能存在,因此未定义步骤错误。

<强>解决方案

要从步骤调用步骤,您需要执行以下操作之一:

1)使用step并确保不包含Given / When / Then关键字:

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

2)或者如果您希望包含Given / When / Then,请改为使用steps

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

请参阅Cucumber wiki

答案 2 :(得分:0)

在这种情况下,提供的课程材料可以解释差异,但没有引导我们在非常相似的两个步骤之间进行描述;更改“当我取消选中以下内容...”时“取消选中以下内容...”允许web_steps.rb返回范围并按需执行(之前链接的帖子link: cucumber contradictory error messages)有更多详细信息)。