As I do guess this is as close to the right answer I will get it is (unfortunately) not what I’m after. The difference being that, as far as I understand, there is no way to not specify which exception specifically to expect.
I would like to find a way to just throw stuff it should not except at the function and then rest assured that I did crash in a controlled way.
As I see it if you specify an expected exception a bit of the Erlang magic is gone when you have to predict the exact behavior and thus only cover one of all the (infinite) possible errors.
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