黄瓜红宝石救援例外情景名称

时间:2016-12-09 12:33:45

标签: ruby exception-handling cucumber capybara

我正在编写一些使用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多个表,那么异常处理并不会告诉您更多关于故障发生的位置。

我想捕获方案名称并将其包含在异常处理中,但是我收到一个错误,说明方案的未定义方法。

有没有办法可以将其添加到代码块中?

由于

1 个答案:

答案 0 :(得分:0)

The more I have read, the more I realised this couldn't be done the way I wanted. So I did the following:

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
相关问题