Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Mock - how to name mock functions?

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?

like image 476
Mawg says reinstate Monica Avatar asked Sep 11 '15 16:09

Mawg says reinstate Monica


People also ask

What is difference between On_call and Expect_call?

So use ON_CALL by default, and only use EXPECT_CALL when you actually intend to verify that the call is made.

How do you make a mock gMock?

Using the Turtle interface as example, here are the simple steps you need to follow: Derive a class MockTurtle from Turtle . Take a virtual function of Turtle (while it's possible to mock non-virtual methods using templates, it's much more involved). In the public: section of the child class, write MOCK_METHOD();

What is gMock and Gtest?

In real system, these counterparts belong to the system itself. In the unit tests they are replaced with mocks. Gtest is a framework for unit testing. Gmock is a framework imitating the rest of your system during unit tests.

What is mock_method0?

You must always put a mock method definition ( MOCK_METHOD* ) in a public: section of the mock class, regardless of the method being mocked being public , protected , or private in the base class. This allows ON_CALL and EXPECT_CALL to reference the mock function from outside of the mock class. (


1 Answers

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.
like image 114
BЈовић Avatar answered Oct 11 '22 23:10

BЈовић