Streamlines setting up and using meck-based mocks
Pull request Compare This branch is 5 commits behind chef-boneyard:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


This is a (somewhat) hacky attempt at making meck just a bit easier to use. My motivation to streamline and automate meck is based on two recent experiences: mocking out a complex database API to reduce testing dependencies and helping new Erlang programmers come up to speed with Eunit and meck.

automeck has two main features. The first is a data driven format for generating simple mock functions which pattern match on their arguments to select the correct output. The file priv/mocks.config is an example of how to configure automeck to generate a set of mocks.

The second feature allows automeck to record function calls. automeck records the module & function name, the call parameters, and the return value. The file priv/record.config is an example of how to configure automeck to record function calls on a set of modules. After you've collected enough data you can tell automeck to generate a set of mocks from the recorded data. The Erlang shell session listed below illustrates the API for this feature in more detail.

1> {ok, Session} = automeck:record("priv/record.config").
%% Let's generate a bunch of function calls
2> [{foo:test1(X), foo:test1(X, X + random:uniform(100)), bar:test(), bar:test(X, X)} || X <- lists:seq(1, 5)].
%% Let's look at the recorded data
3> {ok, RawData} = file:read_file(Session).
{ok, <<...>>}
4> rp(RawData).
<<"{foo, test1, [{[1,53], -52}]}.
   {bar, test, [{[], bar}]}.
   {bar, test, [{[1,1], 2}]}.
   {foo, test1, [{[2,56], -108}]}.
   {bar, test, [{[], bar}]}.
   {bar, test, [{[2,2], 4}]}.
   {foo, test1, [{[3,34], -93}]}...">>

%% Finish the recording session and generate mock config file
5> {ok, MockConfig} = automeck:finish_recording(Session).
6> MockedModules = automeck:mock(MockConfig).
[bar, foo]
%% Let's call a couple of mocked functions and an unmocked one
7> foo:test1(1, 53).
8> bar:test(2, 2).
%% The process tree crashes because this function wasn't mocked
%% Same crash would occur if a mocked function was called with
%% arguments not listed in mocks.config
9> bar:test(1).
=ERROR REPORT==== 27-Aug-2011::15:07:46 ===
** Generic server foo_meck terminating
** Last message in was {'EXIT',<0.31.0>,

automeck started out as an experimental hack but I think these features are generally useful for testing and understanding non-trivial Erlang systems.

automeck's lack of docs, specs, and tests will be addressed either as I have time or as I receive pull requests. Bug reports and/or suggestions also welcome :-)