19

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?

Filip Haglund
  • 13,919
  • 13
  • 64
  • 113

1 Answers1

27

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.

Filip Haglund
  • 13,919
  • 13
  • 64
  • 113
  • 2
    Yes, `assert_rise` is not a macro, but normal function: https://github.com/elixir-lang/elixir/blob/f4a378f158b00aae0f2ee1c211eccc4314ecec01/lib/ex_unit/lib/ex_unit/assertions.ex#L498 This means that if you just pass `myFn(a, b)` it will be evaluated and the value will be passed to `assert_rise` which is already too late :) – tkowal Jun 08 '16 at 14:12