Skip to content

Mock not being called after I passthrough #120

@marbru

Description

@marbru

I'm having some extrange behavior when doing passthroughs. My situation is a module with a main function that calls a subfunction. I would like to keep the original behaviour of the main, but mock the sub.

To put it in code, a module like this:

-module(mymod).

-export([main/0,
    sub/0]).

% I want to passthrough this one
main() ->
    sub().

% I want to mock this one
sub() ->
    sub_original.

If my test code is like this:

my_test() ->
    meck:new(mymod),
    meck:expect(mymod,main,fun()-> meck:passthrough([]) end),
    meck:expect(mymod,sub,0,sub_mock),
    erlang:display(mymod:main()),
    meck:unload(mymod),
   ?_assert(true).

Then erlang:display displays this:

sub_original

If I want to keep the behavior of main, I have no other option than to replicate it in the mock, instead of doing a passthrough:

meck:expect(weird,main,fun()->weird:sub() end)

And then I get the display I wanted:

sub_mock

I suspect that this happens because when I passthrough, the control is passes to the original module, which then calls itself (is it's code indicates), instead of calling the mock. I'm not sure if this is the intended behavior by design, but it's not the one I would have expected.

Is a fix for this plausible?

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions