@ tx34 is the key to the answer, but there are a few more problems in the code.
First, the most important documents are the choice between overloaded functions . You have three fooMethod overloads with the same number of arguments, but with different types of arguments. You will have to use a match that defines the type.
Next, you need to define all of your Foo functions that must be virtualized, or if you call them through the Foo object, the derived mock functions will not call. Since you define Foo as a base class, it must also have a virtual destructor to avoid slicing.
Finally, you need FooMock inherit FooMock from Foo .
Therefore, putting everything together, you will get something like:
#include <memory> #include <string> #include "gtest/gtest.h" #include "gmock/gmock.h" using ::testing::_; using ::testing::An; using ::testing::Matcher; using ::testing::TypedEq; using ::testing::Return; struct Foo { virtual ~Foo() {} virtual int fooMethod(const int&) { return 0; } virtual int fooMethod(const float&) { return 0; } virtual int fooMethod(const std::string&) { return 0; } }; struct FooMock : Foo { FooMock() : Foo() { ON_CALL(*this, fooMethod(An<const int&>())). WillByDefault(Return(-1)); ON_CALL(*this, fooMethod(Matcher<const float&>(_))). WillByDefault(Return(-2)); ON_CALL(*this, fooMethod(TypedEq<const std::string&>("1"))). WillByDefault(Return(-3)); } MOCK_METHOD1(fooMethod, int(const int& intParam)); MOCK_METHOD1(fooMethod, int(const float& floatParam)); MOCK_METHOD1(fooMethod, int(const std::string& stringParam)); }; TEST(Foo, foo) { std::shared_ptr<Foo> foo(new FooMock); auto foo_mock(std::dynamic_pointer_cast<FooMock>(foo)); EXPECT_CALL(*foo_mock, fooMethod(Matcher<const int&>(_))).Times(1); EXPECT_CALL(*foo_mock, fooMethod(Matcher<const float&>(_))).Times(1); EXPECT_CALL(*foo_mock, fooMethod(Matcher<const std::string&>(_))).Times(1); EXPECT_EQ(-1, foo->fooMethod(1)); EXPECT_EQ(-2, foo->fooMethod(1.0f)); EXPECT_EQ(-3, foo->fooMethod("1")); } int main(int argc, char **argv) { testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); }
source share