【问题标题】:cucumber declarative step definitions using web_steps.rb使用 web_steps.rb 的黄瓜声明性步骤定义
【发布时间】:2012-11-23 08:05:15
【问题描述】:

将 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”成功;有人知道可能导致这种行为的原因吗?声明性步骤的实现甚至已经被简化,因此不会发生变量替换...

【问题讨论】:

    标签: ruby-on-rails ruby cucumber


    【解决方案1】:

    我试过了:

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

    然后在steps文件中:

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

    .. 似乎对我有用。

    【讨论】:

    • 您的“puts”命令是否触发了“features/definitions/web_steps.rb”中已定义的步骤的使用?在这种情况下,课堂作业旨在引导我们朝着相同的方向前进……
    • 好吧,已经几天了,还没有任何回复,所以也许一个指向另一个问题的链接(有更多说明)会有所帮助...link: cucumber contradictory error messages
    • “puts”只是为了调试进入步骤的内容。因此 arg1 将具有值“G,PG-13”。但是,您可能应该将 arg1 重命名为更能描述即将发生的内容。我可能误解了,但我认为您的问题是获得一个工作步骤匹配器,然后让您捕获评级并对其运行测试......
    • 是的,您正确理解了问题,您的建议绝对是一个好主意(我正在做类似的事情,方法是短路变量评估并尝试使用常量调用取消检查步骤);关于根本原因的解决方案已在上面和链接的帖子中发布...
    • 如果您已经解决了您的问题,您愿意接受其中一个答案吗?
    【解决方案2】:

    问题

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

    第二行代码:

    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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-07-12
        • 1970-01-01
        • 2014-01-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多