[erlang-questions] Mock functions

Dominic Williams xpdoka@REDACTED
Mon Sep 4 22:53:38 CEST 2006


Arnaud Bailly wrote:

> For unit testing my java code, I use mock objects for all
> interfaces I need in the context of the OUT. This allows
> me to precisely control what's goinig in and out of my
> object.
> 
> I could think of several ways to do this in Erlang but
> they all seem a bit contrived. Is there something
> "canonical" for doing this kind of tests ?

In OO, mock objects are often used to verify that during a 
call, the OUT is pulling the right strings on other objects 
that it carries in its state.

In functional programming, one aims to avoid such side 
effects: a function's return value should only depend on 
input parameters. Such functions are a lot easier to test, 
and don't require the complication of mocks at all.

In OO, mock objects can also be used to verify that the OUT 
is delegating part of its behaviour to an interface: the 
mock or stub implements that interface to get used by the OUT.

In functional programming, the "delegation" would happen by 
passing to the function another function that performs part 
of the work. In this case, lambda is the ultimate mock! In 
your test, you can provide the function under test with a 
dummy fun, a lot more easily than you would write a mock.

I'd be happy to address a specific example if you have a 
problem at hand...

Regards,

Dominic Williams
http://www.dominicwilliams.net

----



More information about the erlang-questions mailing list