2

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?

Don Branson
  • 13,631
  • 10
  • 59
  • 101

2 Answers2

7

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.

Cody Poll
  • 7,690
  • 3
  • 27
  • 22
4

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.
legoscia
  • 39,593
  • 22
  • 116
  • 167