【问题标题】:cucumber ruby rescue exception Scenario namecucumber ruby​​ 救援异常 场景名称
【发布时间】:2016-12-09 12:33:44
【问题描述】:

我正在编写一些使用 Cucumbers 数据表的测试。它遍历 11 个场景,如果第 1 行出现故障,则忽略以下内容(如我们所知)。权力希望我为此添加一些异常处理,以便无论如何都要检查整个表。这是我的桌子:

 | Row | add_line_1    | add_line_2 | add_line_3     | post_town | post_code | error                                                                                                                              |
      | 1   | 123           | Over There | And down a bit | Swansea   | SA9 2NG   | Building/number and street must contain between 4 and 30 farts                                                                |
      | 2   | 1234          | Over There | And down a bit | Swansea   | SA9 2NG   | Must have at least 3 alpha characters                                                                                              |
      | 3   | ****          | Over There | And down a bit | Swansea   | SA9 2BG   | The first character of the address must be alphanumeric. Other characters must be valid (a-z, A-Z, 0-9, &, -, (), /, ' and , or .) |
      | 4   | ***           | Over There | And down a bit | Swansea   | SA9 2BG   | Building/number and street must contain between 4 and 30 characters                                                                |
      | 5   | 1 High Street | ***        | And down a bit | Swansea   | SA9 2BG   | The first character of the address must be alphanumeric. Other characters must be valid (a-z, A-Z, 0-9, &, -, (), /, ' and , or .) |
      | 6   | 1 High Street | Over There | ***            | Swansea   | SA9 2BG   | The first character of the address must be alphanumeric. Other characters must be valid (a-z, A-Z, 0-9, &, -, (), /, ' and , or .) |
      | 7   | 1 High Street | Over There | And down a bit | ****      | SA9 2BG   | Post town contains invalid characters                                                                                              |
      | 8   | 1 High Street | Over There | And down a bit | ***       | SA9 2BG   | Post town contains invalid characters                                                                                              |
      | 9   | 1 High Street | Over There | And down a bit | A         | SA9 2BG   | Post town requires a minimum length of three characters                                                                            |
      | 10  | 1 High Street | Over There | And down a bit | Swansea   | ***       | Must be between five and eight characters and in a valid format, e.g. AB1 2BA or AB12BA      

测试通常在第一行失败,因为预期的错误文本不正确。

所以我把它写进了我的代码中:

begin
    expect(all_text).to have_text @error
rescue Exception => e
  puts "#{scenario.name} >>"
  puts "Table row #{@row}: #{e}"
end

如果您认为整个测试包中有 40 多个表,则异常处理本身并不能告诉您发生故障的位置。

我想捕获场景名称并将其包含在异常处理中,但我收到一条错误消息,提示未定义场景的方法。

有没有办法将它添加到代码块中?

谢谢

【问题讨论】:

    标签: ruby exception-handling cucumber capybara


    【解决方案1】:

    我读的越多,我就越意识到这不能按照我想要的方式完成。所以我做了以下事情:

    Before do |scenario|
      @feature_name = scenario.feature.name
      @scenario_name = scenario.name
    end
    
    begin
      expect(all_text).to have_text @error
      rescue Exception => e
      puts @feature_name + ' : ' + @scenario_name
      puts "Table row #{@row}: #{e}"
    end
    

    【讨论】:

    • 如果您不希望它引发异常,请不要使用期望。如果 all_text_ 是一个元素,则使用 has_text? 代替,如果它是一个字符串,则使用 ==,if
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-24
    • 2012-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-11
    • 1970-01-01
    相关资源
    最近更新 更多