Susan Potter
snippets: Created / Updated

Erlang Meck API notes

Notes on using Meck API (a mocking library in Erlang) which are not well documented.

% Will create a fully mocked version of existing_module until caller crashes
meck:new(ExistingModule).

% Will create a fully mocked version of existing_module even after caller crashes
meck:new(ExistingModule, [nolink]).

% Will allow you to overload existing module, keeping old functions around
meck:new(ExistingModule, [passthrough]).

% Unload mocks and revert to real module implementation
meck:unload(ExistingModule).

% Remove functions from the mock module
meck:delete(WhateverMockedModule, SomeFun, Arity).

% Get list of all function calls made to the module (in order).
History = meck:history(MockedModule).
% History will look _similar_ to an Erlang trace but with {{M, F, A}, Return} format
% e.g. where MockedModule = my_mod
% [
%  {{my_mod, print, []}, ok},
%  {{my_mod, sum, [1,2,3]}, 6},
%  {{my_mod, copy_file, ["non_existing_file", "other_file_name"]}, error, enoent, Stack}
% ]

If you enjoyed this content, please consider sharing this link with a friend, following my GitHub, Twitter/X or LinkedIn accounts, or subscribing to my RSS feed.