Home > Back-end >  How can we get NiceMock<> behavior to apply to a single method?
How can we get NiceMock<> behavior to apply to a single method?

Time:09-22

When a mocked method is called without an expectation set, gmock emits a warning:

GMOCK WARNING:
Uninteresting mock function call - returning default value.
    Function call: getAddress(@0x563e6bbf5530 16-byte object <F0-3A 32-6B 3E-56 00-00 30-A7 BF-6B 3E-56 00-00>)
          Returns: ""
NOTE: You can safely ignore the above warning unless this call should not happen.  Do not suppress it by blindly adding an EXPECT_CALL() if you don't mean to enforce the call.  See https://github.com/google/googletest/blob/master/docs/gmock_cook_book.md#knowing-when-to-expect for details.

Such warnings can be suppressed by wrapping the mock with NiceMock<> before injecting it. Is it possible to get the same effect, but only for some of the methods on the mock?

For example, consider this mock (written using an older version of gtest):

class PocoMock : public Poco
{
public:
    PocoMock() //
        : Poco()
    {
    }

    MOCK_CONST_METHOD1(listen, void(Poco::Net::ServerSocket& rSocket));
    MOCK_CONST_METHOD1(start, void(Poco::Net::TCPServer& rServer));
    MOCK_CONST_METHOD1(stop, void(Poco::Net::TCPServer& rServer));

    MOCK_CONST_METHOD1(
            getAddress,
            std::string(const Poco::Net::StreamSocket& rSocket));
}

Is it possible to tell gmock to suppress warnings about unexpected calls to getAddress(), but not about listen(), start() and stop()?

CodePudding user response:

You can explicitly said you can have any number of call to getAddress:

EXPECT_CALL(mock, getAddress(testing::_)).Times(testing::AnyNumber());

Demo

  • Related