I have some code that looks like this:
-module(ca_data).
-export([delete_ca_data/1]).
% ...
delete_ca_data(N) when N < 1 -> ?NEGATIVE_ID_ERROR;
delete_ca_data(N) -> util_db_generic:delete_object(ca_data, N, fun(_) -> ok end).
% ...
And I have some test code that looks like this:
wrap_meck(MockMod, MockArgs, F) ->
meck:new(MockMod, MockArgs),
try
F()
catch Ex ->
throw(Ex)
after
meck:unload(MockMod)
end.
delete_ca_data_test() ->
F = fun() ->
meck:expect(util_db_generic, delete_object, fun (_, _, _) -> ok end),
?assertEqual(?NEGATIVE_ID_ERROR, ca_data:delete_ca_data(-1)),
?assertEqual([], meck:history(util_db_generic)),
meck:expect(util_db_generic, delete_object, fun(_, _, _) -> ok end),
?assertEqual(ok, ca_data:delete_ca_data(1)),
?assertEqual(
[{self(), {util_db_generic, delete_object, [ca_data, 1, fun(_) -> ok end]}, ok}], % Uh-oh
meck:history(util_db_generic)
)
end,
wrap_meck(util_db_generic, [], F).
Unfortunately, the util_db_generic:delete_object
function is called with a function created in the module under test.
This provides some issues when trying to assert the history of the function calls (for example, on the line marked "Uh-oh"). Since this function is created in-situ (and the memory pointer is effectively random), it's challenging to assert what this function should look like. This code does not result in any compilation error. However, the asserts will never return as valid, since there are two different functions being asserted against each other (one created in the ca_data
module, and one in the testcase). I tried using ?assertMatch
and changing the fun(_) -> ok end
's out for _
s, but I am getting this compilation error on the assert line:
illegal pattern
How can I match these results?
While the Erlang documentation is usually good, this is one point where it lets the user down. The
assertMatch
documentation states:And the documentation for
self()
states:However, the
self()
function cannot be used this theassertMatch
macro. I am unclear on why, but experiment has proved this out.EDIT 1:
The plot thickens! When I edit the test code to look like this:
the code compiles and runs correctly! This is, to me, a surprising result! The function (that should be able to be used in guards) is not allowed, but the return from the function is! Interesting!