What is the expected syntax for checking exception messages in MiniTest's assert_raises
/must_raise
?
I'm trying to make an assertion something like the following, where "Foo"
is the expected error message:
proc { bar.do_it }.must_raise RuntimeError.new("Foo")
You can use the assert_raises
assertion, or the must_raise
expectation.
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