@xcombinator
- I realize there are a million already, but I created another git cheatsheet: http://bit.ly/bfAKlZ 2010/09/01
-
Recent Posts
Recent Comments
- ActiveRecord from_json and from_xml (5)
- Terence: Dude, you are the bomb. Thanks for your fix. Helped us out heaps.
- djb daemontools with Ubuntu’s upstart (2)
- sorcess: such config may lead to data loss… consider above configuration with this little change start on...
- Mac OS X color showing ESC[whatever for git-diff colors (and more) (15)
- automate installing tripwire using expect (1)
- Trey Henefield: I came across this as useful. But I found an even easier solution. There is an option that disables...
- ActiveRecord from_json and from_xml (5)
Categories
- bookmarks (2)
- cascading (2)
- code (2)
- crawling (1)
- deployment (6)
- ec2 (3)
- erlang (2)
- gems (3)
- git (7)
- hadoop (3)
- java (1)
- merb (1)
- music (1)
- osx (2)
- poolparty (3)
- processing (1)
- programming (48)
- rails (11)
- ruby (21)
- scalability (5)
- shell (8)
- sysadmin (16)
- tips (13)
- Uncategorized (3)
- useless (1)
Archives
Pages
Blogroll

testing erlang gen_server with gen_server_mock
Testing by synchronous pattern matching
Testing multi-process erlang gen_servers can be tricky. Typically one relies simply on pattern matching to verify that the response matches what you would expect.
As long as the
gen_servercallhireturnsexpectedas the first element of the tuple, then the tests pass.The technique is also the same when building client-server code where both client and server are
gen_servers. The common case is to simply test one side at a time; test the response of all client calls and then (independently) test the responses of the server calls.What about asynchronous
cast?gen_server:callis convenient because it is synchronous and returns a value.gen_server:cast, on the other hand, is asynchronous and always returns the atomok. This can makecasts difficult to test.gen_server_mockis a library to mockgen_serverprocesses that expect specific, ordered sets of messages. It allows you to unit testgen_servers by verifying they are receiving the expected set of messages.Example 1
This
Mockexpects twocalls:{foo, hi}and{bar, bye}. SinceMockreceives both of these messages,assert_expectationsdoes not raise any errors.What is verified
gen_server_mock:assert_expectations(Mock)verifies that:You can catch the
exitby using the following:Example 2
Currently three types of messages are supported:
call,cast, andinfo.The signature of the
funof each expectation is the same as the correspondinggen_server:handle_*. So thefunforexpect_callhas the same signature ashandle_call:fun(Request, From, State). Seeman gen_serverfor more information.However, the return value of the
funmust be one of:ok | {ok, NewState} | {ok, ResponseValue, NewState} |Anything else will be an error. Note that you can change the state of your
Mockby returningNewState.Arbitrary, non-
gen_servermessages are handled withexpect_info, e.g.Mock ! catfulfills theexpect_infoin the example above.References