[erlang-questions] Mocking in Erlang

Christian S <>
Tue Jun 3 20:57:03 CEST 2008

On Tue, Jun 3, 2008 at 7:54 PM, Per Melin <> wrote:
> Not necessarily. Since modules aren't linked the gorilla module will
> call *whatever* module that calls itself banana at the time the test
> runs, right? So what if your test setup purges the real banana module
> and loads a mock banana with code:load_abs/1? It won't allow you to
> use a generic consumable module though.

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.

>> IBrowse:request(URL)
> I think that calling a function with a variable module like that is
> still slower than a normal qualified function call.
> I did a quick test now and (unless I messed up) it was about 5x
> slower. In most cases that won't matter, but it's something to keep in
> mind.

Not a show-stopper, but bad news enough.  You dont want it to be so
slow that the hairiest code most in need of tests avoid testability in
the name of speed. A 5x slowdown was a lot worse than my guesstimate.

JIT compilers get a lot of their benefits from memoizing lookups like
these, it would be sweet if BEAM could do that too.  Module parameters
stay the same once the module "instance" has been created.

More information about the erlang-questions mailing list