Mirror of Apache CouchDB

Clone this repo:
  1. 3544aca Update README.md by Adam Lindberg · 4 years ago main master
  2. 2754b5c Update for latest version and Rebar 3 by Adam Lindberg · 4 years ago
  3. cb155ca Update copyright notices by Adam Lindberg · 4 years ago
  4. 2dbccc1 Update Changelog for version 0.8.8 by Adam Lindberg · 4 years ago
  5. 1dd41ae Version 0.8.8 by Adam Lindberg · 4 years ago 0.8.8

Travis Hex.pm Version Hex.pm License Erlang Versions Build Tool


A mocking library for Erlang.


See what's new in 0.8 Release Notes.

  • Dynamic return values using sequences and loops of static values
  • Compact definition of mock arguments, clauses and return values
  • Pass through: call functions in the original module
  • Complete call history showing calls, return values and exceptions
  • Mock validation, will invalidate mocks that were not called correctly
  • Throwing of expected exceptions that keeps the module valid
  • Throws an error when mocking a module that doesn't exist or has been renamed (disable with option non_strict)
  • Support for Hamcrest matchers
  • Automatic backup and restore of cover data
  • Mock is linked to the creating process and will unload automatically when a crash occurs (disable with option no_link)
  • Mocking of sticky modules (using the option unstick)


Here's an example of using Meck in the Erlang shell:

Eshell V5.8.4  (abort with ^G)
1> meck:new(dog, [non_strict]). % non_strict is used to create modules that don't exist
2> meck:expect(dog, bark, fun() -> "Woof!" end).
3> dog:bark().
4> meck:validate(dog).
5> meck:unload(dog).
6> dog:bark().
** exception error: undefined function dog:bark/0

Exceptions 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:

5> meck:expect(dog, meow, fun() -> meck:exception(error, not_a_cat) end).
6> catch dog:meow().
7> meck:validate(dog).

Normal 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):

8> meck:expect(dog, jump, fun(Height) when Height > 3 ->
                             (Height) ->
9> dog:jump(2).
10> catch dog:jump(5).
11> meck:validate(dog).

Here's an example of using Meck inside an EUnit test case:

my_test() ->
    meck:expect(my_library_module, fib, fun(8) -> 21 end),
    ?assertEqual(21, code_under_test:run(fib, 8)), % Uses 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.

Eshell V5.8.4  (abort with ^G)
1> meck:new(string, [unstick, passthrough]).
2> string:strip("  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):

Eshell V5.8.4  (abort with ^G)
1> meck:new(string, [unstick]).
2> meck:expect(string, strip, fun(String) -> meck:passthrough([String]) end).
3> string:strip("  test  ").
4> meck:unload(string).
5> string:strip("  test  ").


Meck is best used via Rebar 3. Add Meck to the test dependencies in your rebar.config for your project:

{profiles, [
    {test, [
        {deps, [
            {meck, "0.8.8"}

Manual Build

Meck uses Rebar 3. To build Meck go to the Meck directory and simply type:

rebar3 compile

In order to run all tests for Meck type the following command from the same directory:

rebar3 eunit

Documentation can be generated through the use of the following command:

rebar3 edoc

Test Output

Normally the test output is hidden, but if EUnit is run directly, two things might seem alarming when running the tests:

  1. Warnings emitted by cover
  2. An exception printed by SASL

Both are expected due to the way Erlang currently prints errors. The important line you should look for is All XX tests passed, if that appears all is correct.


Meck will have trouble mocking certain modules since Meck works by recompiling and reloading modules. Since Erlang have a flat module namespace, replacing a module has to be done globally in the Erlang VM. This means certain modules cannot be mocked. The following is a non-exhaustive list of modules that can either be problematic to mock or not possible at all:

  • erlang
  • os
  • crypto
  • compile
  • global
  • timer (possible to mock, but used by some test frameworks, like Elixir's ExUnit)

Also, a meck expectation set up for a function f does not apply to the module- local invocation of f within the mocked module. Consider the following module:

-export([a/0, b/0, c/0]).

a() ->

b() ->

c() ->

Note how the module-local call to c/0 in a/0 stays unchanged even though the expectation changes the externally visible behaviour of c/0:

3> meck:new(test, [passthrough]).
4> meck:expect(test,c,0,changed).
5> test:a().
6> test:b().
6> test:c().


Patches are greatly appreciated! For a much nicer history, please write good commit messages. Use a branch name prefixed by feature/ (e.g. feature/my_example_branch) for easier integration when developing new features or fixes for meck.

Should you find yourself using Meck and have issues, comments or feedback please create an issue here on GitHub.

Meck has been greatly improved by [many contributors] (https://github.com/eproxus/meck/graphs/contributors)!


If you or your company use Meck and find it useful, Bitcoin donations are greatly appreciated!