Search code examples
c++testinghippomocks

HippoMocks: How to ExpectCall With an argument passed by reference?


While using Hippomocks, we are having difficulties expecting that a byref argument was passed to a function.

class A
{
public:
    virtual ~A();
    virtual void foo(IBar& callback, const unsigned x);
};

IBar being an interface.

I then have a function that calls:

void Service::baz()
{a.foo(callback, 1);}

Here's the test:

TEST_FIXTURE(PigScalesServiceTest, test_something)
{
    A* aMock = mockRepository.Mock<A>();
    IBar* callback = mockRepository.Mock<IBar>(); 
    Service service(*amock, *callback);

    mockRepository.ExpectCall(aMock, A::foo).With(*callback, 1);
    service.baz();
}

Running the test will give me the following error:

Function A::foo(???,1) called with mismatching expectation!
Expections set:
Expectation for A::foo(???,1) on the mock at 0x0x10969908 was not satisfied.

Thanks a lot!


Solution

  • I think you should use something like this:

    mockRepository.ExpectCall(aMock, A::foo).With(byRef(*callback), 1);

    Look at the unit tests of HippoMocks itself: https://github.com/dascandy/hippomocks/blob/master/HippoMocksTest/test_ref_args.cpp