My suggestion was not entirely right. You could write a expect_failure macro like this:
defmodule ExpectFailure do
defmacro expect_failure(ast) do
quote do
try do
unquote(ast)
flunk("This should has crashed: " <> unquote(Macro.to_string(ast)))
rescue
e in ExUnit.AssertionError ->
raise e
_ ->
:ok
end
end
end
end
defmodule AsdfTest do
use ExUnit.Case
doctest Asdf
import ExpectFailure
def foo(x) when is_number(x) do
x + 1
end
test "expect_failure catches errors" do
expect_failure(foo("a"))
end
test "expect_failure should crash" do
assert_raise ExUnit.AssertionError, fn ->
expect_failure(foo(1))
end
end
end






















