[erlang-questions] meck

107 views
Skip to first unread message

Roberto Ostinelli

unread,
Jul 22, 2012, 10:15:42 PM7/22/12
to Erlang
dear all,

i'm trying to use the meck framework (https://github.com/eproxus/meck) and i can't seem to find a way to test that a function gets called with the appropriate values.

for instance:

%%%%%%%%%%%%%%%%%%%%

-module(example).
-compile(export_all).

get_config() ->
mocked:myfun("test.config").

-ifdef(TEST).
-include_lib("eunit/include/eunit.hrl").

get_config_test_() ->
meck:new(mocked),
meck:expect(mocked, myfun, fun(Filename) -> ok end),
?_assertEqual(ok, get_config()),
?_assert(meck:validate(mocked)).
-endif.

%%%%%%%%%%%%%%%%%%%%

i'd like to test that mocked:myfun/1 gets called with "test.config" as parameter.

is this possible?

r.

Slava Yurin

unread,
Jul 23, 2012, 12:37:05 AM7/23/12
to Roberto Ostinelli, Erlang
From meck readme.md:
 
my_test() ->
    meck:new(my_library_module),
    meck:expect(my_library_module, fib, fun(8) -> 21 end),
    ?assertEqual(21, code_under_test:run(fib, 8)), % Uses my_library_module
    ?assert(meck:validate(my_library_module)),
    meck:unload(my_library_module).
 
23.07.2012, 09:15, "Roberto Ostinelli" <rob...@widetag.com>:

_______________________________________________
erlang-questions mailing list
erlang-q...@erlang.org
http://erlang.org/mailman/listinfo/erlang-questions

Motiejus Jakštys

unread,
Jul 23, 2012, 4:07:59 AM7/23/12
to Roberto Ostinelli, Erlang
On Mon, Jul 23, 2012 at 4:15 AM, Roberto Ostinelli <rob...@widetag.com> wrote:
> dear all,

>
> for instance:
>
> %%%%%%%%%%%%%%%%%%%%
>
> -module(example).
> -compile(export_all).
>
> get_config() ->
> mocked:myfun("test.config").
>
> -ifdef(TEST).
> -include_lib("eunit/include/eunit.hrl").
>
> get_config_test_() ->
> meck:new(mocked),
> meck:expect(mocked, myfun, fun(Filename) -> ok end),
> ?_assertEqual(ok, get_config()),
> ?_assert(meck:validate(mocked)).
> -endif.

Hi,
you are mixing eunit generators with simple test cases. Did you mean this?

get_config_test() ->


meck:new(mocked),
meck:expect(mocked, myfun, fun(Filename) -> ok end),

?assertEqual(ok, get_config()),
?assert(meck:called(mocked, myfun, ["test.config"])),
meck:unload(mocked).

Normally, you should split meck:new and meck:unload to separate
setup/cleanup functions. Because if a test fails and the module is not
cleaned, it might unexpectedly do ill for your other test cases.

--
Motiejus Jakštys

Roberto Ostinelli

unread,
Jul 25, 2012, 1:30:55 AM7/25/12
to Motiejus Jakštys, Erlang
Hi,
you are mixing eunit generators with simple test cases. Did you mean this?

get_config_test() ->
  meck:new(mocked),
  meck:expect(mocked, myfun, fun(Filename) -> ok end),
  ?assertEqual(ok, get_config()),
  ?assert(meck:called(mocked, myfun, ["test.config"])),
  meck:unload(mocked).

Normally, you should split meck:new and meck:unload to separate
setup/cleanup functions. Because if a test fails and the module is not
cleaned, it might unexpectedly do ill for your other test cases.

can't i use this in generators?  

the reason why i didn't include unload is because of this: https://github.com/eproxus/meck/issues/72

r.
 

Motiejus Jakštys

unread,
Jul 27, 2012, 3:32:18 AM7/27/12
to erlang-q...@erlang.org
Forwarding to list.


---------- Forwarded message ----------
From: Motiejus Jakštys <desir...@gmail.com>
Date: Wed, Jul 25, 2012 at 9:31 AM
Subject: Re: [erlang-questions] meck
To: Roberto Ostinelli <rob...@widetag.com>


On Wed, Jul 25, 2012 at 7:30 AM, Roberto Ostinelli <rob...@widetag.com> wrote:
>
> can't i use this in generators?

You can, but another way. Generator must return a set of test cases.
For instance:

arith_test_() ->
[
?_assertEqual(4, 2*2),
?_assertEqual(0, 1*0)
].

From eunit manual[1]:
A function with a name ending in ..._test_() (note the final
underscore) is recognized by EUnit as a test generator function. Test
generators return a representation of a set of tests to be executed by
EUnit.

Stick with the simple test case instead (which ends with test()).


> the reason why i didn't include unload is because of this:
> https://github.com/eproxus/meck/issues/72

As said, try simple test case first.

[1]: http://www.erlang.org/doc/apps/eunit/chapter.html#EUnit_macros

--
Motiejus Jakštys

Reply all
Reply to author
Forward
0 new messages