15

I am just getting started with Google Mock. The For Dummies is reasonably easy to follow.

However, I don't understand why the example has

class MockTurtle : public Turtle {
 public:
  ...
  MOCK_METHOD0(PenUp, void());
  MOCK_METHOD0(PenDown, void());
  MOCK_METHOD1(Forward, void(int distance));
  MOCK_METHOD1(Turn, void(int degrees));
  MOCK_METHOD2(GoTo, void(int x, int y));
  MOCK_CONST_METHOD0(GetX, int());
  MOCK_CONST_METHOD0(GetY, int());
};

There are multiple MOCK_METHOD0 and multiple MOCK_METHOD1, etc. Functions with similar signatures seem to get the same mock number, but documentation does not mention this, explaining the how & why, and the definitions are identical. How am I to know what to do?

273K
  • 29,503
  • 10
  • 41
  • 64
Mawg says reinstate Monica
  • 38,334
  • 103
  • 306
  • 551
  • 1
    On a glimpse, it seems that the number indicates the number of arguments the mocked methods take. –  Sep 11 '15 at 16:18
  • 1
    People voting to close as "primarily opinion-based" question do not understand what the question is. – BЈовић Sep 11 '15 at 16:19
  • 3
    Update: In recent versions of gmock, the suffix for the argument is not needed. `MOCK_METHOD` is used for all mocked functions. – aschepler Feb 22 '21 at 14:01

1 Answers1

20

In the How to Define It section, it is explained :

  1. In the public: section of the child class, write MOCK_METHODn(); (or MOCK_CONST_METHODn(); if you are mocking a const method), where n is the number of the arguments; if you counted wrong, shame on you, and a compiler error will tell you so.
BЈовић
  • 62,405
  • 41
  • 173
  • 273