[erlang-questions] emock: create mock gen_servers and gen_fsms

Christian chsu79@REDACTED
Sat Feb 28 01:50:29 CET 2009


I started a little project tonight to create a quick mock of gen
behaviors (yeah, code is 4 hours old now).

http://github.com/noss/emock

%% Simple stateless mock
echo_server(call, X) ->
  {reply, X}.
%% Using the mock to create a gen_server
example() ->
  Server = emock:gen_server(fun echo_server/2),
  foo =:= gen_server:call(Server, foo).

As you see it is much shorter to create a mock like this than to
create a full-blown gen_server module for a test.

I have code to support mocks with state as well. Ironically, being a
testing tool, I have not tested it properly yet.

It can be used in eunit like this:

echo_server_test_() ->
  {setup,
    fun () -> emock:gen_server(fun echo_server/2) end,
    fun(Server) -> exit(Server, normal) end,
    fun(Server) -> [
      ?_assertEqual(42, gen_server:call(Server, 42)),
      ?_assertEqual(foo, gen_server:call(Server, foo))
      ]
    end}.

I think emock can be a useful tool to let tests depend on instead of
the actual undocumented gen protocols. It is just 100 lines of code,
but it is nice to not have to redo this for each project. Maybe it
could become a part of eunit if it proves itself useful.

http://noss.github.com/2009/02/27/quick-and-useful-mocking-of-otp-gen-behaviors.html



More information about the erlang-questions mailing list