Search code examples
c++googlemock

How do I verify that 2 mock methods are called with same reference?


Suppose I am testing a method that declares some internal variable (on stack). The variable is passed by reference to one object (method setupFoo()) to fill it with the right values, then passed by reference to another object (method useFoo()) to use the values.

How can I write my EXPECT_CALLs and matchers to verify that both calls to the mocked methods get the same reference? Right now I simply use _ to ignore the references.


Solution

  • You might do something like:

    const void* ref = nullptr;
    EXPECT_CALL(mock, setupFoo(_)).WillOnce(Invoke([&](const auto& ptr) { ref = &ptr;}));
    EXPECT_CALL(mock, useFoo(_)).WillOnce(Invoke([&](auto& ptr) { EXPECT_EQ(ref, &ptr);}));