黄瓜声明步骤定义使用网络
在使用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安装后生成cucumber_rails_training_wheels:install'创建的文件),其中包含命令性的默认步骤,如:
When /^(?:|I )uncheck "([^"]*)"$/ do |field|
check(field)
end
此外,自从添加When I uncheck "ratings_G"
到'features / step_definitions / movie_steps.rb'成功后,它似乎可以通过Cucumber访问'features / step_definitions / web_steps.rb'; 任何人都知道什么可能导致这种行为? 声明步骤的实现甚至已经简化,因此不会发生变量替换...
我试过了:
When I uncheck the following ratings: "G, PG-13"
然后在步骤文件中:
When /^I uncheck the following ratings: "([^"]*)"$/ do |arg1|
puts "arg1: #{arg1}"
end
..似乎为我工作。
问题
问题在于你如何尝试从另一个步骤调用某个步骤。
代码的第二行:
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"
}
查看Cucumber wiki。
在这种情况下,提供的课程材料阐明了差异,但并没有导致我们在两个非常相似的步骤之间进行划分; 将“取消选中以下内容...”更改为“取消选中以下内容...”后,web_steps.rb将回到范围并根据需要执行(以前链接的发布,链接:黄瓜相互矛盾的错误消息,更多细节)。
链接地址: http://www.djcxy.com/p/82609.html上一篇: cucumber declarative step definitions using web
下一篇: Print feature file names while running all the cucumber features