[erlang-questions] Mocking in Erlang

Robert Virding <>
Sun Jun 8 12:03:20 CEST 2008


It is definitely a very powerful tool which should be used carefully.
However, as this works on a per process basis you can use it in an app by
using processes to split on which error handling method to use. Processes
rule.

My comment was really that it is not a dirty hack, but a valid way of doing
things.

Robert

2008/6/8 Hynek Vychodil <>:

> Nice and elegant idea, but it is not solution for running non mocked and
> mocked code. When you run some code calling normal banana:eat, you can't run
> in same time process with mocked  version because error_handler will not be
> raced. So for carefully wrote test it can be enough, but very fragile. You
> must disallow loading normal banana module by using embedded version of
> global error_handler.
>
>
> On Sat, Jun 7, 2008 at 6:57 PM, Christian S <> wrote:
>
>> > True. And something I would like even better is if I could generate
>> > that module in a few lines close to the eunit test function. (Cant be
>> > impossible to create a module directly from a list of atoms and funs.
>> > Doesnt need to be fast.)
>> >
>> > A downside is that the module namespace is node-wide, so you cant
>> > safely run your tests in parallell on the same node. This is a reason
>> > I consider this to be a very dirty way to slip in the mock.
>>
>> To follow up on myself. I hacked up something that almost fulfills my
>> wishes:
>>
>> You load this module that follows the inteface of an error_handler:
>>
>> -module(mock).
>>
>> -compile(export_all).
>>
>> undefined_function(Mod, F, Args) ->
>>    case get(Mod) of
>>        undefined ->
>>            exit({undefined, Mod, F, Args});
>>        MockDict ->
>>            Fun = dict:fetch(F, MockDict),
>>            apply(Fun, Args)
>>    end.
>>
>> undefined_lambda(_Mod, _Fun, _Args) ->
>>    exit(undef).
>>
>>
>> Second, I put this in my process to install the above error_handler
>> for this process:
>>
>> init() ->
>>    Mock = dict:from_list([{eat, fun() -> yum end}]),
>>    put(banana, Mock),
>>    process_flag(error_handler, mock),
>>    loop().
>>
>>
>> Whenever this process calls banana:eat(), it will get the atom 'yum'
>> back.  Completly process local.
>>
>>
>>
>> YES! It is dirty!  The prettiest flowers grow in manure!
>>
>>
>> PS.
>> http://paste.lisp.org/display/61885 has more of the code and
>> experiment, at least until it goes away in a couple of hours.
>> _______________________________________________
>> erlang-questions mailing list
>> 
>> http://www.erlang.org/mailman/listinfo/erlang-questions
>>
>
>
>
> --
> --Hynek (Pichi) Vychodil
> _______________________________________________
> erlang-questions mailing list
> 
> http://www.erlang.org/mailman/listinfo/erlang-questions
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20080608/05bf6cfe/attachment.html>


More information about the erlang-questions mailing list