[erlang-questions] Have been looking for mock and stub

Michal Ptaszek <>
Wed Mar 2 16:20:31 CET 2011


By any chance, have you tried meck?
https://github.com/esl/meck

On Mar 2, 2011, at 4:02 PM, Allen Kim wrote:

> I have been looking for mock and stub for eunit testing, but I couldn't find a simple one.
> So, I build one by borrowing few codes from open source, and used it in our company for a year and I thought it was good enough.
> 
> Here is the code that I use and share,  https://github.com/epicadvertising/mock_stub
> And the following is the example of usage;
> 
> 
> Eshell V5.7.3 (abort with ^G)
> 
>> {ok, _Pid} = mock:start(my_module).
>> mock:expect(my_module, foo, [], mock_foo0).
>> mock:expect(my_module, 'foo/1', mock_other_foo1).
>> my_module:foo().
> mock_foo0
>> my_module:foo(1).
> mock_other_foo1
>> mock:stop(my_module).
>> my_module:foo().
> foo0
> 
>> gen_server:start({local, my_reg_gen_server}, my_gen_server, [], []).
>> gen_server:call(my_reg_gen_server, foo).
> call_foo
>> mock:start(my_gen_server).
>> mock:expect(my_gen_server, 'handle_call/3', {reply, mock_call_foo, []}).
>> gen_server:call(my_reg_gen_server, foo).
> mock_call_foo
> 
>> gen_event:start_link({local,my_gen_event_manager}).
>> gen_event:add_handler(my_gen_event_manager, my_gen_event, []).
>> gen_event:call(my_gen_event_manager, my_gen_event, foo).
> call_foo
>> mock:start(my_gen_event).
>> mock:expect(my_gen_event, 'handle_call/2', {ok, mock_call_foo, []}).
>> gen_event:call(my_gen_event_manager, my_gen_event, foo).
> mock_call_foo
> 
> 
> I hope this is useful for somebody who is looking for simple mock and stub, and if not please lt me know.
> 
> Allen



More information about the erlang-questions mailing list