ruby 在 MiniTest 的 assert_raises/must_raise 中检查异常消息的预期语法是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14418628/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 05:40:35  来源:igfitidea点击:

What is the expected syntax for checking exception messages in MiniTest's assert_raises/must_raise?

rubytddminitestassertion

提问by kfitzpatrick

What is the expected syntax for checking exception messages in MiniTest's assert_raises/must_raise?

在 MiniTest 的assert_raises/ 中检查异常消息的预期语法是什么must_raise

I'm trying to make an assertion something like the following, where "Foo"is the expected error message:

我正在尝试做出如下断言,"Foo"预期的错误消息在哪里:

proc { bar.do_it }.must_raise RuntimeError.new("Foo")

回答by blowmage

You can use the assert_raisesassertion, or the must_raiseexpectation.

您可以使用assert_raises断言或must_raise期望。

it "must raise" do
  assert_raises RuntimeError do 
    bar.do_it
  end
  ->     { bar.do_it }.must_raise RuntimeError
  lambda { bar.do_it }.must_raise RuntimeError
  proc   { bar.do_it }.must_raise RuntimeError
end

If you need to test something on the error object, you can get it from the assertion or expectation like so:

如果您需要在错误对象上测试某些内容,您可以从断言或期望中获取它,如下所示:

describe "testing the error object" do
  it "as an assertion" do
    err = assert_raises RuntimeError { bar.do_it }
    assert_match /Foo/, err.message
  end

  it "as an exception" do
    err = ->{ bar.do_it }.must_raise RuntimeError
    err.message.must_match /Foo/
  end
end

回答by Jing Li

To assert exception:

断言异常:

assert_raises FooError do
  bar.do_it
end

To assert exception message:

断言异常消息:

As per API doc, assert_raisesreturns the exception matched so you can check the message, attributes, etc.

根据API docassert_raises返回匹配的异常,以便您可以检查消息、属性等。

exception = assert_raises FooError do
  bar.do_it
end
assert_equal('Foo', exception.message)

回答by Developer

Minitest does not provide (yet) you a way to check the actual exception message. But you could add a helper method that does it and extend ActiveSupport::TestCaseclass to use everywhere in your rails test suite, e.g.: in test_helper.rb

Minitest 没有提供(还)你一种检查实际异常消息的方法。但是您可以添加一个辅助方法来完成它并扩展ActiveSupport::TestCase类以在您的 Rails 测试套件中的任何地方使用,例如:test_helper.rb

class ActiveSupport::TestCase
  def assert_raises_with_message(exception, msg, &block)
    block.call
  rescue exception => e
    assert_match msg, e.message
  else
    raise "Expected to raise #{exception} w/ message #{msg}, none raised"
  end
end

and use it in your tests like:

并在您的测试中使用它,例如:

assert_raises_with_message RuntimeError, 'Foo' do
  code_that_raises_RuntimeError_with_Foo_message
end

回答by Kostas Rousis

To add some more recent developments, there have been some discussionson adding assert_raises_with_messageto minitest in the past without much luck.

为了添加一些最近的发展,过去有一些关于添加assert_raises_with_message到 minitest 的讨论,但运气不佳。

Currently, there is a promising pull requestwaiting to be merged. If and when it gets merged, we will be able to use assert_raises_with_messagewithout having to define it ourselves.

目前,有一个有希望的拉取请求等待合并。如果并且当它合并时,我们将能够使用assert_raises_with_message而无需自己定义它。

In the meanwhile, there is this handy little gem named minitest-bonus-assertionswhich defines exactly that method, along with few others, so that you can use it out of the box. See the docsfor more information.

同时,有一个名为minitest-bonus-assertions 的方便的小宝石,它准确定义了该方法以及其他一些方法,因此您可以开箱即用。有关更多信息,请参阅文档