------------------------- testing module: -module(meck_test). -compile(export_all). test()-> ok = meck:new(meck_module_for_test,[passthrough]), % Docs says: Retains the original functions, if not mocked by meck.
% Mock "a()" which is called from call_a(). ok = meck:expect(meck_module_for_test, a, 0, fun() -> "result_from_mocked_a" end ),
Result = meck_module_for_test:call_a(), io:format("Result of call_a: ~p~n",[Result]),
Result_A = meck_module_for_test:a(), io:format("Result of a: ~p~n",[Result_A]),
--------------------- result of meck_test:test(): Result of call_a: a Result of a: #Fun<meck_test.1.126597877> Valid: true ok -----------------------------
I believe that call_a() should call a mocked version of a() which should return "result_from_mocked_a". So the result from call_a() shoud be:"result_from_mocked_a" But my results are different.
You can only mock external calls using meck, this means that you must prefix the call to a() in meck_module_for_test:call_a/0 with meck_module_for_test: or ?MODULE: to ensure that the call is handled by the mocked version of the module. When you make a local call to a function in the same module it will always call the function defined in the current module version.
MVH Magnus
On Tue, Jan 10, 2012 at 4:47 PM, Bohuslav Svancara <bsvanc...@gmail.com>wrote:
> ------------------------- testing module: > -module(meck_test). > -compile(export_all). > test()-> > ok = meck:new(meck_module_for_test,[passthrough]), % Docs says: > Retains the original functions, if not mocked by meck.
> % Mock "a()" which is called from call_a(). > ok = meck:expect(meck_module_for_test, a, 0, fun() -> > "result_from_mocked_a" end ),
> Result = meck_module_for_test:call_a(), > io:format("Result of call_a: ~p~n",[Result]),
> Result_A = meck_module_for_test:a(), > io:format("Result of a: ~p~n",[Result_A]),
> --------------------- result of meck_test:test(): > Result of call_a: a > Result of a: #Fun<meck_test.1.126597877> > Valid: true > ok > -----------------------------
> I believe that call_a() should call a mocked version of a() which should > return "result_from_mocked_a". > So the result from call_a() shoud be:"result_from_mocked_a" > But my results are different.
> You can only mock external calls using meck, this means that you must > prefix the call to a() in meck_module_for_test:call_a/0 with > meck_module_for_test: or ?MODULE: to ensure that the call is handled by > the mocked version of the module. When you make a local call to a function > in the same module it will always call the function defined in the current > module version.
> MVH Magnus
> On Tue, Jan 10, 2012 at 4:47 PM, Bohuslav Svancara <bsvanc...@gmail.com>wrote:
>> Hello! >> I am trying to use meck, but have no luck. >> Can you help me, please?
>> ------------------------- testing module: >> -module(meck_test). >> -compile(export_all). >> test()-> >> ok = meck:new(meck_module_for_test,[passthrough]), % Docs says: >> Retains the original functions, if not mocked by meck.
>> % Mock "a()" which is called from call_a(). >> ok = meck:expect(meck_module_for_test, a, 0, fun() -> >> "result_from_mocked_a" end ),
>> Result = meck_module_for_test:call_a(), >> io:format("Result of call_a: ~p~n",[Result]),
>> Result_A = meck_module_for_test:a(), >> io:format("Result of a: ~p~n",[Result_A]),
>> --------------------- result of meck_test:test(): >> Result of call_a: a >> Result of a: #Fun<meck_test.1.126597877> >> Valid: true >> ok >> -----------------------------
>> I believe that call_a() should call a mocked version of a() which should >> return "result_from_mocked_a". >> So the result from call_a() shoud be:"result_from_mocked_a" >> But my results are different.
Comparing the output in the two posts, the first one returns 'a' from call_a() and the return value you specified using meck:expect/4 from the a() function, the second one shows that both call_a() and a() returns the value you specified.
If you want to return "result_from_mocked_a" from these functions you must invoke meck:expect/3 or /4 as:
meck:expect(meck_module_for_test, a, 0, "result_from_mocked_a") or meck:expect(meck_module_for_test, a, fun() -> "result_from_mocked_a" end )
MVH Magnus
On Wed, Jan 11, 2012 at 12:27 AM, Bohuslav Svancara <bsvanc...@gmail.com>wrote:
> Comparing the output in the two posts, the first one returns 'a' from > call_a() and the return value you specified using meck:expect/4 from the > a() function, the second one shows that both call_a() and a() returns the > value you specified.
> If you want to return "result_from_mocked_a" from these functions you > must invoke meck:expect/3 or /4 as:
> meck:expect(meck_module_for_test, a, 0, "result_from_mocked_a") or > meck:expect(meck_module_for_test, a, fun() -> "result_from_mocked_a" end )
> MVH Magnus
> On Wed, Jan 11, 2012 at 12:27 AM, Bohuslav Svancara <bsvanc...@gmail.com>wrote:
>> Thank you, but it does not help.
>> ---------- modified tested module:
>> -module(meck_module_for_test). >> -compile(export_all). >> a() -> a. >> call_a() -> *meck_module_for_test:a().* % changed line
>> ------- testing module "meck_test" is the same (se below)
>> Result:
>> 1> meck_test:test(). >> Result of call_a: #Fun<meck_test.1.84433456> >> Result of a: #Fun<meck_test.1.84433456> >> Valid: true >> ok