I have a class which as one of its private member has an std::function, something like this:
class MyClass
{
private:
int val;
std::function<void(int)> foo;
public:
MyClass(int p_val, std::function<void(int)> p_foo)
{
this->val = p_val;
this->foo = p_foo;
}
void aMethod(int input)
{
this->foo(input);
}
};
I want to test that when function aMethod is called, the callback got called anyway. For this I'm doing a test fixture in gtest that looks like this:
TEST_F(SomeFixture, MyTest)
{
const int input;
::testing::MockFunction<void(int)> mockCallback;
EXPECT_CALL(mockCallback, Call(_));
MyClass object(0, mockCallback.AsStdFunction());
object.aMethod(1);
}
This seems to work because the test passes and if I comment out object.aMethod(1)
in the test or this->foo(input);
in the class the the test fails. What is unclear to me is the role of Call(_)
: I understand that _
corresponds to the input parameter but what does it mean and how does it work?
::testing::_
is a wildcard matcher, meaning that mock calls do not verify what is passed as argument (as long as compiler allows it).
Other matchers could be passed instead, e.g.
EXPECT_CALL(mockCallback, Call(1));
// or
EXPECT_CALL(mockCallback, Call(Eq(1)));
would verify that mockCallback
was called with argument 1
.