0

将 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

有问题的特定配置在“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' 文件(即,在 gem 安装后由 'rails generate cucumber_rails_training_wheels:install' 创建的文件),具有如下命令式默认步骤:

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

此外,Cucumber 似乎可以访问 'features/step_definitions/web_steps.rb',因为添加When I uncheck "ratings_G"到 'features/step_definitions/movie_steps.rb' 成功;有人知道可能导致这种行为的原因吗?声明性步骤的实现甚至已被简化,因此不会发生变量替换......

4

3 回答 3

0

我试过了:

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

然后在步骤文件中:

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

..似乎对我有用。

于 2012-11-23T09:41:40.423 回答
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

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

解决方案

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

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"
  }

请参阅黄瓜维基

于 2012-11-27T03:23:33.560 回答
0

在这种情况下,所提供的课程材料阐明了差异,但并未引导我们在两个非常相似的步骤之间进行描述;将“当我取消选中以下...”更改为“当取消选中以下...”允许 web_steps.rb 回到范围并根据需要执行(之前链接的帖子,链接:黄瓜矛盾错误消息,有更多细节)。

于 2012-12-03T19:38:08.183 回答