Skip to content

Commit

Permalink
Keep storing history when reloading a mock
Browse files Browse the repository at this point in the history
Fixes #194.
  • Loading branch information
eproxus committed Aug 8, 2018
1 parent a5138a3 commit 70e3e30
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 23 deletions.
19 changes: 6 additions & 13 deletions src/meck_proc.erl
Original file line number Diff line number Diff line change
Expand Up @@ -300,20 +300,13 @@ handle_cast(invalidate, S) ->
{noreply, S#state{valid = false}};
handle_cast({add_history, HistoryRecord}, S = #state{history = undefined,
trackers = Trackers}) ->
UpdTracker = update_trackers(HistoryRecord, Trackers),
{noreply, S#state{trackers = UpdTracker}};
UpdTrackers = update_trackers(HistoryRecord, Trackers),
{noreply, S#state{trackers = UpdTrackers}};
handle_cast({add_history, HistoryRecord}, S = #state{history = History,
trackers = Trackers,
reload = Reload}) ->
case Reload of
undefined ->
UpdTrackers = update_trackers(HistoryRecord, Trackers),
{noreply, S#state{history = [HistoryRecord | History],
trackers = UpdTrackers}};
_ ->
% Skip Item if the mocked module compiler is running.
{noreply, S}
end;
trackers = Trackers}) ->
UpdTrackers = update_trackers(HistoryRecord, Trackers),
{noreply, S#state{history = [HistoryRecord | History],
trackers = UpdTrackers}};
handle_cast(_Msg, S) ->
{noreply, S}.

Expand Down
42 changes: 32 additions & 10 deletions test/meck_history_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@
-include_lib("eunit/include/eunit.hrl").

history_test_() ->
{foreach, fun setup/0, fun teardown/1,
[fun num_calls_with_arity/0,
fun capture_different_positions/0,
fun capture_different_args_specs/0,
fun result_different_positions/0,
fun result_different_args_specs/0,
fun result_exception/0,
fun result_different_caller/0
]
}.
{foreach, fun setup/0, fun teardown/1, [
fun num_calls_with_arity/0,
fun capture_different_positions/0,
fun capture_different_args_specs/0,
fun result_different_positions/0,
fun result_different_args_specs/0,
fun result_exception/0,
fun result_different_caller/0,
fun history_kept_while_reloading/0
]}.

setup() ->
%% Given
Expand Down Expand Up @@ -160,3 +160,25 @@ result_different_caller() ->
%% Then
?assertMatch(2003, meck_history:result(2, self(), test, foo, 3)),
?assertMatch(2002, meck_history:result(last, Pid, test, foo, 3)).

history_kept_while_reloading() ->
NumCalls = 10,
meck:new(historical, [non_strict, passtrough]),
meck:expect(historical, test_fn, fun(Arg) -> {mocked, Arg} end),
Test = self(),
Caller = spawn(fun() ->
io:format("~nCalls: ~p~n", [lists:reverse(lists:foldl(fun(N, Acc) ->
timer:sleep(1),
Result = historical:test_fn(N),
[{self(), {historical, test_fn, [N]}, Result}|Acc]
end, [], lists:seq(1, NumCalls)))]),
Test ! {done, self()}
end),
[
meck:expect(historical, test_fn, fun(Arg) -> {mocked2, Arg} end)
|| _ <- lists:seq(1, 5)
],

receive {done, Caller} -> ok after 1000 -> error(caller_timeout) end,
io:format("History: ~p~n", [meck:history(historical)]),
?assertEqual(NumCalls, meck:num_calls(historical, test_fn, '_')).

0 comments on commit 70e3e30

Please sign in to comment.