[erlang-questions] [ANN] gen_server_mock: a simple gen_server mocking library

Christian chsu79@REDACTED
Tue Aug 11 21:46:34 CEST 2009


Myself I made this: http://github.com/noss/emock it does not have
expect-reply-assert as yours. It is plain internal state and manual
checking of messages and their order. And no end assert on conditions,
just crash and bring the test down as soon as something is wrong.

My only feedback to you is that it would be nice if the code could
look a little more tabular. Breaking it down like this and passing
them all in one go could perhaps give it more "overview:ness".

  [{call, fun(one,  _From, _State) -> ok end},
   {call, fun(two,  _From,  State) -> {ok, State} end},
   {call, fun(three, _From,  State) -> {ok, good, State} end},
   {call, fun({echo, Response},_From, State) -> {ok, Response, State} end},
   {cast, fun(fish, State) -> {ok, State} end},
   {info, fun(cat,  State) -> {ok, State} end}]

I find that I want my test code to be very tabular, any other way and
it feels like I should have test cases for my testcases because it
looks too messy to be obviously correct.

Question to anyone: Is it possible to use parse-transforms to minimize
the cruft in having to write out the fun? (They are here for their
pattern matching capability.)

On Tue, Aug 11, 2009 at 21:00, Nate Murray<nate@REDACTED> wrote:
> 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
> ________________________________________________________________
> erlang-questions mailing list. See http://www.erlang.org/faq.html
> erlang-questions (at) erlang.org

More information about the erlang-questions mailing list