Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elixir assert_raise doesn't catch exceptions

Tags:

elixir

ex-unit

I wrote this test case:

assert_raise ArgumentError, myFn(a,b)

but it does not evaluate in the way I'd expect. myFn raises an ArgumentError (do: raise ArgumentError), but it is not caught by assert_raise.

The example in the docs works just fine:

assert_raise ArithmeticError, fn ->
  1 + "test"
end

From the documentation:

assert_raise(exception, function)
Asserts the exception is raised during function execution. Returns the rescued exception, fails otherwise

I'm guessing that in my test case, the arguments are evaluated first. But how should I've written it?

like image 768
Filip Haglund Avatar asked Jun 08 '16 13:06

Filip Haglund


1 Answers

Wrapping the function call in a function is the way to go.

assert_raise ArgumentError, fn ->
  myFn(a, b)
end

I expected assert_raise to take a function call, but it takes a function.

like image 165
Filip Haglund Avatar answered Oct 15 '22 13:10

Filip Haglund