I am using an Agent for state. Nothing I have tried for asserting an ArgumentError
raised in the Agent has worked! The test fails and the process dies without the assertion working.
Here’s some code that demonstrates. In this function add_action
, I want to verify that the value of the map key :actions
is a list, and hasn’t been changed to something else.
def start_link do
Agent.start_link(@name, :init, [], name: @name)
end
def init do
%{actions: []}
end
def add_action(action) when is_atom(action) do
Agent.get_and_update(@name, fn(map) ->
result = Map.update!(map, :actions, fn
list when is_list(list) -> [action | list]
_ -> raise ArgumentError, message: "key :actions is not a list!"
end)
{result, result}
end)
end
Here is the test:
setup do
{:ok, pid} = State.start_link
[pid: pid]
end
test "action example", context do
# assume State.actions is `42` here instead of `[]`
assert_raise ArgumentError, fn ->
# the process dies here, so `assert_raise` is never called
State.add_action :failure
end
end
I have tried catch_exit
, catch_error
, catch_throw
, catch_pokemon
, Process.monitor
, receive
, etc. No matter what, I still get this error message
=ERROR REPORT==== 1-Oct-2016::19:39:16 ===
** Generic server 'Elixir.ManOrActionMan.State' terminating
** Last message in was {get_and_update,
#Fun<Elixir.DepsInstall.State.0.35286781>}
*snip*
I can’t seem to figure out how to catch that ArgumentError
, or if that’s even the right approach to raising errors in a process.
Any help is much appreciated!