Elixir assert_raise doesn't catch exceptions

只谈情不闲聊 提交于 2020-02-03 03:56:45

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/37703632/elixir-assert-raise-doesnt-catch-exceptions

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!