[erlang-questions] Mocking I/O calls - defining sequence of returns

James Churchman <>
Sun May 22 00:07:32 CEST 2011


as erlang is functional by default you always get the same result every time
you call a pure function.

you have to either place in a random ( is random a "pure" thing? ) that
randomly selects an output each time, or you need to store the number of
calls in an impure way. The options would be to spawn off a process ( eg a
genserver ) in the background and have your mock code talk to it, start an
ets table, maybe even * write to a file * tho i get that defeats the purpose
of what your trying to do, or as a cheep trick use the process dictionary:

first put a starting value in the PD :
put(number_of_runs,0)
then in your code you can :
case Runs = get(number_of_runs) & put(number_of_runs,Runs+1)
& erase at the end : erase(number_of_runs)
http://www.erlang.org/course/advanced.html#dict

tho use of the process dictionary makes debugging& understanding the code
etc.... very tricky and breaks most of the elegant parts of erlang, so its
best used for the kinds of uses you have in mind and less so in your real
code :-)

On 21 May 2011 19:55, Edward Wang <> wrote:

> Then you will implement your mock version of io:get_line that way. The mock
> framework merely dispatches the call. That's all.
> On May 22, 2011 2:40 AM, "Alyssa Kwan" <> wrote:
> > I'm sorry, I wasn't being specific. Is there any way to mock a sequence
> of calls to the same function? Like if I call io:get_line (or a wrapper
> because it's protected), how do I get it to return a different value for the
> first call and the second call?
> >
> > Also, will it expect exactly two calls as well? And does meck record call
> order for different functions?
> >
> > On May 21, 2011, at 1:43 PM, Edward Wang wrote:
> >
> >> Try meck https://github.com/eproxus/meck
> >>
> >> I used it to mock inet:getifaddrs/0 etc.
> >>
> >> On Sun, May 22, 2011 at 1:31 AM, Alyssa Kwan <>
> wrote:
> >> Hi everyone!
> >>
> >> Are there any mocking libraries written that allow you to expect a
> sequence of calls and return a different value for each call? A mocking
> library that allows passthrough would be ideal.
> >>
> >> Thanks!
> >> Alyssa
> >> _______________________________________________
> >> erlang-questions mailing list
> >> 
> >> http://erlang.org/mailman/listinfo/erlang-questions
> >>
> >
>
> _______________________________________________
> erlang-questions mailing list
> 
> http://erlang.org/mailman/listinfo/erlang-questions
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20110521/a47c5a1a/attachment.html>


More information about the erlang-questions mailing list