If you want to mock OTP behaviours, then I'd suggest looking at <a href="https://github.com/noss/emock">https://github.com/noss/emock</a>, which is really good for that.<br><br><div class="gmail_quote">On 7 November 2011 03:19, Jon Watte <span dir="ltr"><<a href="mailto:jwatte@gmail.com">jwatte@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">When it comes to mocking, I know that this is one option, and a number of people are recommending meck. However, both of these use the "sort it all out afterwards" approach, which I personally feel is not strict enough. When you mock, you want to mock for a few reasons:<div>
1) You want plausible data to be returned from unrelated modules. You don't really need to go to a disk mnesia table to test your configuration parser :-)</div><div>2) You want to enforce that, when functions are called, parameters conform to certain parameters. This may be "is exactly x,z,y" or just "is numeric" or whatever.</div>
<div>3) You want to enforce the presence or absence of specific calls with specific parameters.</div><div><br></div><div>I find that the "setup" then "replay" mechanism of erlymock (by Jason Wagner) is almost ideally suited to these cases -- and, once I mocked, and run my code, the system automatically verifies my expectations; I don't have to go trawling through any captured results of calls to try to verify whether they are good or not!</div>
<div>Now, in cases where you want to mock gen_server, or erlang, the code replacement approach used just won't work. I don't think there's much of a way around that, other than to use shallow wrappers in your own code, and mock those wrappers.</div>
<div>Personally, I really wish that module got some more public love in the Erlang community. To me, it does it close to Right(tm).</div><div><br></div><div>Sincerely,</div><div><br></div><div>jw</div><div><span class="HOEnZb"><font color="#888888"><br clear="all">
<br>--<br>Americans might object: there is no way we would sacrifice our living standards for the benefit of people in the rest of the world. Nevertheless, whether we get there willingly or not, we shall soon have lower consumption rates, because our present rates are unsustainable. <br>
</font></span><div><div class="h5">
<br>
<br><br><div class="gmail_quote">On Sun, Nov 6, 2011 at 3:19 AM, Klas Johansson <span dir="ltr"><<a href="mailto:klas.johansson@gmail.com" target="_blank">klas.johansson@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Hi Adam,<br>
<br>
On Fri, Nov 4, 2011 at 4:54 PM, Adam Lindberg<br>
<<a href="mailto:adam.lindberg@erlang-solutions.com" target="_blank">adam.lindberg@erlang-solutions.com</a>> wrote:<br>
<br>
> Nice to see more mocking tools popping up!<br>
<br>
:-)<br>
<br>
> - Is it tied to EUnit or generic?<br>
<br>
It's generic (the ?MOCK macro is generic, although the<br>
?WITH_MOCKED_SETUP macro is intended for use from eunit).<br>
<br>
> - What happens when a function is called with other parameters than those expected?<br>
<br>
Nothing. If I write a "guard" like this:<br>
<br>
?WAS_CALLED(lists:nth(2, [a, b, c])),<br>
<br>
that call will succeed if the function was called with those<br>
parameters once. If it was called with those parameters for example<br>
twice (or never) it'll fail (like an assert macro in eunit). If it<br>
was called once with those parameters, and once with another set of<br>
parameters it still succeeds.<br>
<br>
If you want to check that the function was only called once with those<br>
parameters and no other parameters you can do something like this:<br>
<br>
[[2, [a, b, c]]] = ?WAS_CALLED(lists:nth(_, _)),<br>
<br>
since ?WAS_CALLED (as well as ?WAIT_CALLED and ?GET_CALLS) will always<br>
return a list of argument lists.<br>
<br>
Not as concise. Perhaps a better syntax for those cases will make it<br>
into a future version. :-)<br>
<br>
> - How's the beam renaming working out for you so far? Have you seen any cases in<br>
> where it hasn't been able to find all occurrences of a module name?<br>
<br>
So far it's done its job, but it's not really bullet proof. Haven't<br>
had the need to call the original module frequently enough to bump<br>
into problems. The algorithm only replaces the atom within the atom<br>
table, but not within the constant pool. mockgyver works for modules<br>
both with and without debug_info, but I've been thinking of changing<br>
that:<br>
<br>
* use debug_info when available<br>
* otherwise, resort to replacing within the beam code<br>
<br>
It's not been an issue so far, but if people start using this and bump<br>
into problems I'll prioritize it.<br>
<br>
> - really nice syntax (thanks to parse transforms).<br>
<br>
Thanks!<br>
<br>
> Is there a functional API?<br>
<br>
Yes and no. There is, but it's not documented and may change.<br>
However, nothing is set in stone. If there are good reasons for adding<br>
such an interface it'll be done.<br>
<br>
Cheers,<br>
<font color="#888888">Klas<br>
</font><div><div></div><div><br>
> On 2011-11-02, at 21:47 , Klas Johansson wrote:<br>
><br>
>> Hi all,<br>
>><br>
>> I'd like you to meet mockgyver -- an Erlang tool which will make it<br>
>> easier to write EUnit tests that need to replace or alter (stub/mock)<br>
>> the behaviour of other modules.<br>
>><br>
>> mockgyver aims to make that process as easy as possible with a<br>
>> readable and concise syntax.<br>
>><br>
>> mockgyver is built around two main constructs: ?WHEN which makes it<br>
>> possible to alter the behaviour of a function and another set of<br>
>> macros (like ?WAS_CALLED) which check that a function was called with<br>
>> a chosen set of arguments. Let's redefine pi to 4:<br>
>><br>
>> ?WHEN(math:pi() -> 4),<br>
>> 4 = math:pi(),<br>
>><br>
>> Use pattern matching to check that a function was called with certain arguments:<br>
>><br>
>> ?WAS_CALLED(lists:reverse([a, b, c])),<br>
>><br>
>> ... or if you don't care about the arguments:<br>
>><br>
>> ?WAS_CALLED(lists:reverse(_)),<br>
>><br>
>> The library has been in use for a year for a bunch of Erlang<br>
>> applications, except for a couple of recent additions.<br>
>><br>
>> A short tutorial as well as docs and many more examples[1] in markdown<br>
>> format on github:<br>
>><br>
>> <a href="https://github.com/klajo/mockgyver" target="_blank">https://github.com/klajo/mockgyver</a><br>
>><br>
>><br>
>> Cheers,<br>
>> Klas<br>
>><br>
>> [1] <a href="https://github.com/klajo/mockgyver/blob/master/doc/mockgyver.md" target="_blank">https://github.com/klajo/mockgyver/blob/master/doc/mockgyver.md</a><br>
>> _______________________________________________<br>
>> erlang-questions mailing list<br>
>> <a href="mailto:erlang-questions@erlang.org" target="_blank">erlang-questions@erlang.org</a><br>
>> <a href="http://erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://erlang.org/mailman/listinfo/erlang-questions</a><br>
><br>
><br>
_______________________________________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org" target="_blank">erlang-questions@erlang.org</a><br>
<a href="http://erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://erlang.org/mailman/listinfo/erlang-questions</a><br>
</div></div></blockquote></div><br></div></div></div>
<br>_______________________________________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br>
<a href="http://erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://erlang.org/mailman/listinfo/erlang-questions</a><br>
<br></blockquote></div><br>