Skip to content

Commit 51cb010

Browse files
committed
Merge pull request #3726 from whatyouhide/better-assert-raise-messages
Better assert raise messages
2 parents bee194c + cd9253d commit 51cb010

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

lib/ex_unit/lib/ex_unit/assertions.ex

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -392,8 +392,11 @@ defmodule ExUnit.Assertions do
392392
Regex.regex?(message) -> Exception.message(error) =~ message
393393
end
394394

395-
msg = "Wrong message for #{inspect exception}. " <>
396-
"Expected #{inspect message}, got #{inspect Exception.message(error)}"
395+
msg = "Wrong message for #{inspect exception}\n" <>
396+
"Expected:\n" <>
397+
" #{inspect message}\n" <>
398+
"Got:\n" <>
399+
" #{inspect Exception.message(error)}"
397400
assert is_match, message: msg
398401

399402
error

lib/ex_unit/test/ex_unit/assertions_test.exs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,32 @@ defmodule ExUnit.AssertionsTest do
336336
"Expected exception SyntaxError but got FunctionClauseError (no function clause matching in :lists.flatten/1)" = error.message
337337
end
338338

339+
test "assert raise comparing messages (for equality)" do
340+
assert_raise RuntimeError, "foo", fn ->
341+
raise RuntimeError, "bar"
342+
end
343+
rescue
344+
error in [ExUnit.AssertionError] ->
345+
"Wrong message for RuntimeError" <>
346+
"\nExpected:" <>
347+
"\n \"foo\"" <>
348+
"\nGot:" <>
349+
"\n \"bar\"" = error.message
350+
end
351+
352+
test "assert raise comparing messages (with a regex)" do
353+
assert_raise RuntimeError, ~r/ba[zk]/, fn ->
354+
raise RuntimeError, "bar"
355+
end
356+
rescue
357+
error in [ExUnit.AssertionError] ->
358+
"Wrong message for RuntimeError" <>
359+
"\nExpected:" <>
360+
"\n ~r/ba[zk]/" <>
361+
"\nGot:" <>
362+
"\n \"bar\"" = error.message
363+
end
364+
339365
test "assert greater than operator" do
340366
true = assert 2 > 1
341367
end

0 commit comments

Comments
 (0)