eproxus/meck

Test failing with `not_mocked`

Closed this issue · 4 comments

reconnect_success_test() ->
    meck:new(ejabberd_dekaf_client, [passthrough]),
    meck:expect(ejabberd_dekaf_client, do_connect, fun(_,_,_) -> {ok, pid} end),
    {ok, Pid} = ejabberd_dekaf_client:start_link("foo", 0, <<"bar">>),
    ok.

This test is crashing (after succeeding) with:

=ERROR REPORT==== 8-Jun-2015::20:06:47 ===
** Generic server <0.1602.0> terminating
** Last message in was {'EXIT',<0.1594.0>,normal}
** When Server state == {state,"foo",0,<<"bar">>,pid,true,1}
** Reason for termination ==
** {{not_mocked,ejabberd_dekaf_client},
    [{meck_proc,gen_server,3,[{file,"src/meck_proc.erl"},{line,437}]},
     {meck_code_gen,exec,4,[{file,"src/meck_code_gen.erl"},{line,147}]},
     {gen_server,terminate,6,[{file,"gen_server.erl"},{line,722}]},
     {proc_lib,init_p_do_apply,3,[{file,"proc_lib.erl"},{line,227}]}]}

For some reason, if I shorted the test a bit, it passes:

reconnect_success_test() ->
    meck:new(ejabberd_dekaf_client, [passthrough]),
    meck:expect(ejabberd_dekaf_client, do_connect, fun(_,_,_) -> {ok, pid} end),
    ok.

Thanks for looking.

Since I don't know what ejabberd_dekaf_client:start_link/3 does, it's hard to tell what goes wrong here. Would you happen to have any more stack traces available? I suspect the not_mocked error is thrown from the Meck API (or a call to a mock) from inside your gen_server somewhere (hence the Generic server ... terminating message).

Hm. start_link traps exits, which seems to trigger the error. Here's the whole code:

-module(ejabberd_dekaf_client).
-behavior(gen_server).

start_link(_,_,_) -> gen_server:start_link(?MODULE, {}, []).
init(_) ->
    process_flag(trap_exit, true),
    {ok, foo}.

Could it be that because you trap exits, the process doesn't die when the test is over and it continues to call the mock (which is gone since the test is over)?

Closing because of inactivity. Please re-open if it is still an issue!