googletest icon indicating copy to clipboard operation
googletest copied to clipboard

Made mock modifiers inheritable

Open AKJ7 opened this issue 10 months ago • 1 comments
trafficstars

Motivation

For large codebases, it can be very annoying to define Mocks:

class MockFoo {
 public:
  MOCK_METHOD((std::pair<bool, int>), GetPair, ());
  MOCK_METHOD(bool, CheckMap, ((std::map<int, double>), bool));
};

Using such mocks will create Uninteresting Mock functions warnings when any of the mock methods are used. Possible solutions include implementing EXPECT_CALL for each and every mock methods of this class. This can be very teadious and unnecessary when an actual implementation of the mock methods are not needed.

Alternatively, the NiceMock modifier can be used. This can however create very repetative code (Yes, i know #defines and usings can be used), it would be nice if the modifiers can directly be applied directly to the class definition as so:

class MockFoo: public ::testing::NiceMockable<MockFoo> {
 public:
  MOCK_METHOD((std::pair<bool, int>), GetPair, ());
  MOCK_METHOD(bool, CheckMap, ((std::map<int, double>), bool));
};

This feature is implementated in this pull request and i would be happy if this code can be reviewed and merged.

Do i need to update the gtest/gmock docs?

Thanks.

  • Mock object can be made Strict/Nice/Naggy by inheritance

AKJ7 avatar Dec 26 '24 03:12 AKJ7

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

google-cla[bot] avatar Dec 26 '24 03:12 google-cla[bot]