A mocking library for Erlang.
- Flexible, dynamic expectations
- Adding, listing, merging (with the
merge_expectsoption) and deleting at runtime - Compact definition of function arguments or dynamic matchers, clauses and return values
- Dynamic return values using sequences and loops of static values
- Passthrough calls to the original module
- Exceptions designated as intentional keep the module valid
- Customizable default return value for all functions of the mocked module
(with the
stub_alloption)
- Adding, listing, merging (with the
- Full call history access
- Complete call history showing calls, return values and exceptions
- Capture of individual argument values from specific calls
- Waiting for specific calls to the mock, with a timeout
- History reset
- Disabling of history recording for performance-critical tests (with the
no_historyoption)
- Intuitive mock lifecycle management
- Protection against mocking modules that do not exist (disable with the
non_strictoption) - Automatic mock creation for existing modules when mocking functions
- Invalidation of mocks that are not called correctly
- Automatic unloading when the creating process crashes (disable with the
no_linkoption) - Listing of all current mocks
- Mocking of sticky modules (with the
unstickoption) - Automatic backup and restore of cover data, with the possibility to
disable cover on passthrough calls (with the
no_passthrough_coveroption)
- Protection against mocking modules that do not exist (disable with the
- Batch operations on several modules at once
Here's an example of using Meck in the Erlang shell:
1> meck:new(dog, [non_strict]). % non_strict is used to create modules that don't exist
ok
2> meck:expect(dog, bark, fun() -> "Woof!" end).
ok
3> dog:bark().
"Woof!"
4> meck:validate(dog).
true
5> meck:unload(dog).
ok
6> dog:bark().
** exception error: undefined function dog:bark/0Exceptions can be anticipated by Meck (resulting in validation still passing). This is intended to be used to test code that can and should handle certain exceptions indeed does take care of them:
1> meck:new(dog, [non_strict]).
ok
2> meck:expect(dog, meow, fun() -> meck:exception(error, not_a_cat) end).
ok
3> catch dog:meow().
{'EXIT',{not_a_cat,[...]}}
4> meck:validate(dog).
trueNormal Erlang exceptions result in a failed validation. The following example is just to demonstrate the behavior, in real test code the exception would normally come from the code under test (which should, if not expected, invalidate the mocked module):
1> meck:new(dog, [non_strict]).
ok
2> meck:expect(dog, jump, fun(Height) when Height =< 3 -> ok end).
ok
3> dog:jump(2).
ok
4> catch dog:jump(5).
{'EXIT',{function_clause,[...]}}
5> meck:validate(dog).
falseHere's an example of using Meck inside an EUnit test case:
my_test() ->
meck:new(my_library_module),
meck:expect(my_library_module, fib, fun(8) -> 21 end),
?assertEqual(21, code_under_test:run(fib, 8)), % Uses my_library_module
?assert(meck:validate(my_library_module)),
meck:unload(my_library_module).Pass-through is used when the original functionality of a module should be kept.
When the option passthrough is used when calling new/2 all functions in the
original module will be kept in the mock. These can later be overridden by
calling expect/3 or expect/4.
1> meck:new(string, [unstick, passthrough]).
ok
2> string:strip(" test ").
"test"It's also possible to pass calls to the original function allowing us to
override only a certain behavior of a function (this usage is compatible with
the passthrough option). passthrough/1 will always call the original
function with the same name as the expect is defined in):
1> meck:new(string, [unstick, passthrough]).
ok
2> meck:expect(string, strip, fun
("foo") -> "bar";
(String) -> meck:passthrough([String])
end).
ok
3> string:strip(" test ").
"test"
4> string:strip("foo").
"bar"
5> meck:unload(string).
ok
6> string:strip("foo").
"foo"Meck is best used via Rebar 3. Add Meck to the test dependencies in
your rebar.config:
{profiles, [{test, [{deps, [meck]}]}]}.