[erlang-questions] Mock functions
Tue Sep 5 10:44:40 CEST 2006
Arnaud Bailly wrote:
> So the question is: In erlang, how can I provide "virtual"
> functions ? I think the only way would be to "thread" a
> DB interface record containing functions as field into all
> my business methods. In Haskell, I could write a monad and
> encapsulate all my business logic in a DB Monad for
> abstracting away side-effects on the DB layer. In java, I
> create a reference to a DAO interface that is wired to
> business object at deploy or creation time.
> What is the Erlang way to do this ?
I would create a server (Erlang process or gen_server) that
acts as broker for all data access. It responds to Erlang
messages from other processes. Then, for tests it becomes
trivial to provide a fake implementation which returns
predefined answers, or answers defined by the test cases
You can either pass the Pid of this data server to all
modules and functions that use it, or use a named
(registered) process. Either way makes it easy to provide a
fake server for tests, or to switch to a new implementation
if the backend changes.
More information about the erlang-questions