[erlang-questions] Mocking in Erlang
Hynek Vychodil
vychodil.hynek@REDACTED
Sun Jun 8 10:25:00 CEST 2008
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 <chsu79@REDACTED> 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
> erlang-questions@REDACTED
> http://www.erlang.org/mailman/listinfo/erlang-questions
>
--
--Hynek (Pichi) Vychodil
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20080608/edb1d7c9/attachment.htm>
More information about the erlang-questions
mailing list