[ANN] gen_server_mock: a simple gen_server mocking library

Nate Murray nate@REDACTED
Tue Aug 11 21:00:19 CEST 2009


Hey guys, I was inspired by [this
post](http://erlang.org/pipermail/erlang-questions/2008-April/034140.html)
to
write a simple gen_server mocking library. [Github
Repo](http://github.com/jashmenn/gen_server_mock)

It is by no means complete. Currently it only supports specific
ordered messages (although my hope is to eventually add at-least-N,
at-most-N etc.).
Here's a code example:

         {ok, Mock} = gen_server_mock:new(),

         gen_server_mock:expect_call(Mock, fun(one,  _From, _State)
        -> ok end),
         gen_server_mock:expect_call(Mock, fun(two,  _From,  State)
        -> {ok, State} end),
         gen_server_mock:expect_call(Mock, fun(three, _From,  State)
        -> {ok, good, State} end),
         gen_server_mock:expect_call(Mock, fun({echo, Response},
_From, State) -> {ok, Response, State} end),
         gen_server_mock:expect_cast(Mock, fun(fish, State) -> {ok, State} end),
         gen_server_mock:expect_info(Mock, fun(cat,  State) -> {ok, State} end),

         ok = gen_server:call(Mock, one),
         ok = gen_server:call(Mock, two),
         good = gen_server:call(Mock, three),
         tree = gen_server:call(Mock, {echo, tree}),
         ok   = gen_server:cast(Mock, fish),
         Mock ! cat,

         gen_server_mock:assert_expectations(Mock),
         {ok}.

Download it here: git clone git://github.com/jashmenn/gen_server_mock.git
Introductory blog post:
http://www.xcombinator.com/2009/08/11/testing-erlang-gen_server-with-gen_server_mock/

Feedback and patches welcome.

Nate Murray


More information about the erlang-questions mailing list