README.md

meck

A mocking library for Erlang.

Introduction

With meck you can easily mock modules in Erlang. Since meck is
intended to be used in testing, you can also perform some basic
validations on the mocked modules, such as making sure no function is
called in a way it should not.

meck automatically renames existing modules in case they are loaded
when you want to mock them, and restores them upon unloading of the
mocked module. It is also possible to call the original functions from
a mocked module using meck:passthrough/1 from inside an expectation.

Build

meck requires rebar to build. Either install rebar by building it
manually and putting it in your path or by using Agner (agner
install rebar).

To build meck, got to the meck directory and simply type:

$ rebar compile

Make sure meck works on your platform (requires the eunit
application, which is included by default in Erlang):

$ rebar eunit

Two things might seem alarming when running the tests: 1. Warnings
emitted by cover and 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.

Install

If you want to install your own built version of meck add the ebin
directory to your Erlang code path or move the meck folder into your
release folder and make sure that folder is in your ERL_LIBS
environment variable.

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

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.

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 is
defined in):