GMock - return default with ON_CALL for overloaded methods

I am trying to write mock for a class that contains three overloaded methods, i.e. .:

#include <gtest/gtest.h> #include <gmock/gmock.h> using ::testing::_; using ::testing::Return; using ::testing::A; using ::testing::ByRef; using ::testing::Ref; using ::testing::TypedEq; struct Foo { int fooMethod(const int& intParam) { return 0; } int fooMethod(const float& floatParam) { return 0; } int fooMethod(const std::string& stringParam) { return 0; } }; struct FooMock { FooMock() { ON_CALL(*this, fooMethod(_)).WillByDefault(Return(-1)); } MOCK_METHOD1(fooMethod, int(const int& intParam)); MOCK_METHOD1(fooMethod, int(const float& floatParam)); MOCK_METHOD1(fooMethod, int(const std::string& stringParam)); }; 

but this gives an error:

  error: call of overloaded 'gmock_fooMethod(const testing::internal::AnythingMatcher&)' is ambiguous 

I also tried TypedEq () instead of "_", but it gives more obscure errors. I checked the GMock FAQ, Wiki, and I did not find a solution - how can I return the default value using ON_CALL for overloaded methods?

BR, Lucas

+6
source share
2 answers

@ 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(); } 
+10
source

The problem is that TypedEq expects a value that does not match. You can achieve what you want:

 ON_CALL(*this, fooMethod(An<ArgType>())).WillByDefault(Return(-1)); 

or

 ON_CALL(*this, fooMethod(Matcher<ArgType>(_))).WillByDefault(Return(-1)); 

See also:

https://github.com/google/googletest/blob/master/googlemock/docs/CookBook.md#selecting-between-overloaded-functions

https://github.com/google/googletest/blob/master/googlemock/docs/CheatSheet.md#wildcard

https://github.com/google/googletest/blob/master/googlemock/docs/CheatSheet.md#generic-comparison

+2
source

Source: https://habr.com/ru/post/944702/


All Articles