在MiniTest的assert_raises
/must_raise
中,判断异常消息的预期语法是什么?
我试图做出如下断言,其中"Foo"
是预期的错误消息:
proc { bar.do_it }.must_raise RuntimeError.new("Foo")
在MiniTest的assert_raises
/must_raise
中,判断异常消息的预期语法是什么?
我试图做出如下断言,其中"Foo"
是预期的错误消息:
proc { bar.do_it }.must_raise RuntimeError.new("Foo")
您可以使用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
如果需要在错误对象上测试某些内容,可以从断言或预期中获得,如下所示:
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