Skip to content

Commit

Permalink
Fix calling mocked functions from expectation fun.
Browse files Browse the repository at this point in the history
Before this change calling a mocked function from inside
an expectation fun, and then calling meck:passthrough/1
raises an exception. This happens because the current
function state is invalidated by the mocked function.
  • Loading branch information
pergu committed Aug 19, 2021
1 parent 30f845f commit 7cf1055
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/meck_code_gen.erl
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ exec(Pid, Mod, Func, Args) ->
-spec eval(Pid::pid(), Mod::atom(), Func::atom(), Args::[any()],
ResultSpec::any()) -> Result::any() | no_return().
eval(Pid, Mod, Func, Args, ResultSpec) ->
PreviousCall = get(?CURRENT_CALL),
put(?CURRENT_CALL, {Mod, Func}),
try
Result = meck_ret_spec:eval_result(Mod, Func, Args, ResultSpec),
Expand All @@ -183,7 +184,7 @@ eval(Pid, Mod, Func, Args, ResultSpec) ->
handle_exception(Pid, Mod, Func, Args,
Class, Reason, ?_get_stacktrace_(StackToken))
after
erase(?CURRENT_CALL)
put(?CURRENT_CALL, PreviousCall)
end.

-spec handle_exception(CallerPid::pid(), Mod::atom(), Func::atom(),
Expand Down
6 changes: 6 additions & 0 deletions test/meck_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1376,6 +1376,12 @@ can_mock_sticky_modules_test() ->
?assert(code:is_sticky(meck_test_module)),
code:unstick_mod(meck_test_module).

meck_reentrant_test() ->
meck:new(string, [unstick, passthrough]),
meck:expect(string, strip,
fun(String) -> meck:passthrough([string:reverse(String)]) end),
?assertEqual(string:strip(" ABC "), "CBA"),
meck:unload(string).

sticky_directory_test_() ->
{foreach, fun sticky_setup/0, fun sticky_teardown/1,
Expand Down

0 comments on commit 7cf1055

Please sign in to comment.