-
Recent Posts
Recent Comments
- Mac OS X color showing ESC[whatever for git-diff colors (and more) (12)
- Gopala Krishna A: Thanks a lot!! This really proved helpful on opensuse 11.2
- Silly Avatar: Thanx for this blog entry. Was having this problem while ssh-ing to a linux vps w/Putty. Thought it was...
- Girish KS: Thanks for the post Nate and thanks pablitostar for your suggestion. I started using git few days back and...
- pablitostar: I found using the -r flag did fix git-diff, but it broke something else in less. Specifically, searching...
- Gopala Krishna A: Thanks a lot!! This really proved helpful on opensuse 11.2
- ActiveRecord from_xml (and from_json) part 2 (3)
- Billy Kimble: Thanks for the snippet of code — it has helped me out tremendously. Unfortunately it did not work...
- Mac OS X color showing ESC[whatever for git-diff colors (and more) (12)
Categories
- bookmarks (2)
- cascading (2)
- code (2)
- 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 (43)
- 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