Suppose I have a method
void Mock::foo(const A& obj);
and I want to check that it was called exactly with the object obj
rather than its copy:
A obj;
EXPECT_CALL(mock, foo(obj));
mock->foo(obj);
How can I check this? I found Address(m)
matcher here. But I cannot find it in ::testing
, i.e. it does not compile.
CodePudding user response:
As mentioned in comments, ::testing::Address()
matcher was introduced in GoogleTest 1.11.
You can use instead ::testing::Ref()
matcher, which does the same thing underneath (comparing addresses) and is available since at least GoogleTest 1.8 (see it online):
#include <gmock/gmock.h>
struct Data
{};
struct Mock
{
MOCK_METHOD(void, foo, (const Data& d), ());
};
struct ClassUnderTest
{
Mock* dependency;
void methodUnderTest(const Data& d)
{
dependency->foo(d);
}
void methodThatCopies(const Data& d)
{
Data copy = d;
dependency->foo(copy);
}
};
TEST(X, Correct)
{
Mock m;
Data d;
ClassUnderTest uut {&m};
EXPECT_CALL(m, foo(::testing::Ref(d)));
uut.methodUnderTest(d);
}
TEST(X, Fail)
{
Mock m;
Data d;
ClassUnderTest uut {&m};
EXPECT_CALL(m, foo(::testing::Ref(d)));
uut.methodThatCopies(d);
}
int main(int argc, char** argv)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}