Can meck mock erlang:exit?

478 Views Asked by At

I want this in a supervisor module:

stop() ->
  exit(whereis(mousetrap_sup), kill).

So a naïve test might do this:

stop_invokes_exit_test() ->
  meck:new(erlang, [unstick, passthrough]),
  meck:expect(erlang, whereis, 1, a_pid),
  meck:expect(erlang, exit, 2, true),
  mousetrap_sup:stop(),
  ?assert(meck:called(erlang, exit, [a_pid, kill])).

Not surprisingly, it hangs.

I can see where it might not be possible to exercise this code with a test, but is there a way?

2

There are 2 best solutions below

0
On BEST ANSWER

You could spawn a process using that name, and check the exit reason:

{Pid, Ref} = spawn_monitor(timer, sleep, [infinity]),
register(my_sup, Pid),
mousetrap_sup:stop(),
receive
    {'DOWN', Ref, process, Pid, Reason} ->
        ?assertEqual(killed, Reason)
after 1000 ->
    error(not_killed)
end.
0
On

From the meck documentation

Meck will have trouble mocking certain modules since Meck works by recompiling and reloading modules. Since Erlang have a flat module namespace, replacing a module has to be done globally in the Erlang VM. This means certain modules cannot be mocked. The following is a non-exhaustive list of modules that can either be problematic to mock or not possible at all:

  • erlang
  • os
  • crypto
  • compile
  • global

So no, you can't mock exit. You can, however, wrap the exit call in another function and meck that function.