ruby - 断言 Cucumber 中抛出了一个特定的异常

标签 ruby exception-handling cucumber

场景

我正在编写一个库(没有 Ruby on Rails),我想为其提供非常详细的 Cucumber 功能。这尤其包括描述在各种情况下应该抛出的错误/异常。

示例

编写 Cucumber 步骤最直观的方式可能是这样的

When I do something unwanted
Then an "ArgumentError" should be thrown

问题

我必须解决两个问题:

  1. 抛出异常时,第一步不应失败。
  2. 第一步抛出的异常应该可供第二步访问,以便执行一些断言魔法。

不优雅和繁琐的解决方案

我能想到的最佳方法是在第一步中缓存异常并将其放入第二步可以访问的实例变量中,如下所示:

When /^I do something unwanted$/ do
  begin
    throw_an_exception!
  rescue => @error
  end
end

Then /^an "(.*)" should be thrown$/ do |error|
  @error.class.to_s.should == error
end

但是,在我希望它失败的情况下,这会使第一步或多或少变得无用,并且它需要一个实例变量,这从来都不是一件好事。

那么,任何人都可以帮我解决一个至少不那么麻烦的解决方案吗?还是我应该以不同的方式编写我的功能?任何帮助将不胜感激。

最佳答案

我又想了想,也许答案是:

没有优雅的解决方案,因为 Given-When-Then -您的情况违反了计划。 您期望“然后应该抛出异常”是“当我做一些不想要的事情时”的结果。

但是仔细想想,这不是真的!异常不是此操作的结果,实际上异常只是表明“When”-Statement 失败。

我的解决方案是在更高级别进行测试:

When I do something unwanted
Then an error should be logged

When I do something unwanted
Then the user should get an error message

When I do something unwanted
Then the program should be locked in state "error"

或这些的组合。

然后您将在您的程序中“缓存异常”——这非常有意义,因为您很可能无论如何都需要这样做。

你说的这两个问题也会解决。

万一你真的必须测试异常

嗯,我猜 Cucumber 不是正确的测试套件,嗯? ;-)

由于 Given-When-Then-Scheme 无论如何都被违反了,我会简单地写

When I do something unwanted it should fail with "ArgumentError"

在步骤定义中类似(未经测试,如果您尝试请纠正我)

When /^I do something unwanted it should fail with "(.*)"$/ do |errorstring|
  expect {
    throw_an_exception!
  }.to raise_error(errorstring)
end

如上所述,这是非常错误的,因为方案已被破坏,但它会达到目的,不是吗? ;-)

您将在测试错误中找到更多文档 at rspec expectations .

关于ruby - 断言 Cucumber 中抛出了一个特定的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9219959/

相关文章:

java - cucumber arity 不匹配 : trying to bind several matching groups to a single stepdef parameter

ruby - 选择包含子节点内文本的父节点

ruby - 开始在 Facebook 上使用应用程序?

ruby-on-rails - 使用设计检查用户是否在登录期间确认

java - cucumber junit中的标签继承

html - Cucumber 可以用于表单测试吗

ruby - 合并 ruby​​ 中的 csv 数据以获得唯一值的总计/总和

exception-handling - iOS 5.0 忽略 NSSetUncaughtExceptionHandler?

c# - 为什么在 C# 中捕获并重新抛出异常?

java - 在 Java 中使用 AssertionError 和断言