xref: /freebsd/contrib/googletest/docs/gmock_faq.md (revision 2e3507c25e42292b45a5482e116d278f5515d04d)
1# Legacy gMock FAQ
2
3### When I call a method on my mock object, the method for the real object is invoked instead. What's the problem?
4
5In order for a method to be mocked, it must be *virtual*, unless you use the
6[high-perf dependency injection technique](gmock_cook_book.md#MockingNonVirtualMethods).
7
8### Can I mock a variadic function?
9
10You cannot mock a variadic function (i.e. a function taking ellipsis (`...`)
11arguments) directly in gMock.
12
13The problem is that in general, there is *no way* for a mock object to know how
14many arguments are passed to the variadic method, and what the arguments' types
15are. Only the *author of the base class* knows the protocol, and we cannot look
16into his or her head.
17
18Therefore, to mock such a function, the *user* must teach the mock object how to
19figure out the number of arguments and their types. One way to do it is to
20provide overloaded versions of the function.
21
22Ellipsis arguments are inherited from C and not really a C++ feature. They are
23unsafe to use and don't work with arguments that have constructors or
24destructors. Therefore we recommend to avoid them in C++ as much as possible.
25
26### MSVC gives me warning C4301 or C4373 when I define a mock method with a const parameter. Why?
27
28If you compile this using Microsoft Visual C++ 2005 SP1:
29
30```cpp
31class Foo {
32  ...
33  virtual void Bar(const int i) = 0;
34};
35
36class MockFoo : public Foo {
37  ...
38  MOCK_METHOD(void, Bar, (const int i), (override));
39};
40```
41
42You may get the following warning:
43
44```shell
45warning C4301: 'MockFoo::Bar': overriding virtual function only differs from 'Foo::Bar' by const/volatile qualifier
46```
47
48This is a MSVC bug. The same code compiles fine with gcc, for example. If you
49use Visual C++ 2008 SP1, you would get the warning:
50
51```shell
52warning C4373: 'MockFoo::Bar': virtual function overrides 'Foo::Bar', previous versions of the compiler did not override when parameters only differed by const/volatile qualifiers
53```
54
55In C++, if you *declare* a function with a `const` parameter, the `const`
56modifier is ignored. Therefore, the `Foo` base class above is equivalent to:
57
58```cpp
59class Foo {
60  ...
61  virtual void Bar(int i) = 0;  // int or const int?  Makes no difference.
62};
63```
64
65In fact, you can *declare* `Bar()` with an `int` parameter, and define it with a
66`const int` parameter. The compiler will still match them up.
67
68Since making a parameter `const` is meaningless in the method declaration, we
69recommend to remove it in both `Foo` and `MockFoo`. That should workaround the
70VC bug.
71
72Note that we are talking about the *top-level* `const` modifier here. If the
73function parameter is passed by pointer or reference, declaring the pointee or
74referee as `const` is still meaningful. For example, the following two
75declarations are *not* equivalent:
76
77```cpp
78void Bar(int* p);         // Neither p nor *p is const.
79void Bar(const int* p);  // p is not const, but *p is.
80```
81
82### I can't figure out why gMock thinks my expectations are not satisfied. What should I do?
83
84You might want to run your test with `--gmock_verbose=info`. This flag lets
85gMock print a trace of every mock function call it receives. By studying the
86trace, you'll gain insights on why the expectations you set are not met.
87
88If you see the message "The mock function has no default action set, and its
89return type has no default value set.", then try
90[adding a default action](gmock_cheat_sheet.md#OnCall). Due to a known issue,
91unexpected calls on mocks without default actions don't print out a detailed
92comparison between the actual arguments and the expected arguments.
93
94### My program crashed and `ScopedMockLog` spit out tons of messages. Is it a gMock bug?
95
96gMock and `ScopedMockLog` are likely doing the right thing here.
97
98When a test crashes, the failure signal handler will try to log a lot of
99information (the stack trace, and the address map, for example). The messages
100are compounded if you have many threads with depth stacks. When `ScopedMockLog`
101intercepts these messages and finds that they don't match any expectations, it
102prints an error for each of them.
103
104You can learn to ignore the errors, or you can rewrite your expectations to make
105your test more robust, for example, by adding something like:
106
107```cpp
108using ::testing::AnyNumber;
109using ::testing::Not;
110...
111  // Ignores any log not done by us.
112  EXPECT_CALL(log, Log(_, Not(EndsWith("/my_file.cc")), _))
113      .Times(AnyNumber());
114```
115
116### How can I assert that a function is NEVER called?
117
118```cpp
119using ::testing::_;
120...
121  EXPECT_CALL(foo, Bar(_))
122      .Times(0);
123```
124
125### I have a failed test where gMock tells me TWICE that a particular expectation is not satisfied. Isn't this redundant?
126
127When gMock detects a failure, it prints relevant information (the mock function
128arguments, the state of relevant expectations, and etc) to help the user debug.
129If another failure is detected, gMock will do the same, including printing the
130state of relevant expectations.
131
132Sometimes an expectation's state didn't change between two failures, and you'll
133see the same description of the state twice. They are however *not* redundant,
134as they refer to *different points in time*. The fact they are the same *is*
135interesting information.
136
137### I get a heapcheck failure when using a mock object, but using a real object is fine. What can be wrong?
138
139Does the class (hopefully a pure interface) you are mocking have a virtual
140destructor?
141
142Whenever you derive from a base class, make sure its destructor is virtual.
143Otherwise Bad Things will happen. Consider the following code:
144
145```cpp
146class Base {
147 public:
148  // Not virtual, but should be.
149  ~Base() { ... }
150  ...
151};
152
153class Derived : public Base {
154 public:
155  ...
156 private:
157  std::string value_;
158};
159
160...
161  Base* p = new Derived;
162  ...
163  delete p;  // Surprise! ~Base() will be called, but ~Derived() will not
164                 // - value_ is leaked.
165```
166
167By changing `~Base()` to virtual, `~Derived()` will be correctly called when
168`delete p` is executed, and the heap checker will be happy.
169
170### The "newer expectations override older ones" rule makes writing expectations awkward. Why does gMock do that?
171
172When people complain about this, often they are referring to code like:
173
174```cpp
175using ::testing::Return;
176...
177  // foo.Bar() should be called twice, return 1 the first time, and return
178  // 2 the second time.  However, I have to write the expectations in the
179  // reverse order.  This sucks big time!!!
180  EXPECT_CALL(foo, Bar())
181      .WillOnce(Return(2))
182      .RetiresOnSaturation();
183  EXPECT_CALL(foo, Bar())
184      .WillOnce(Return(1))
185      .RetiresOnSaturation();
186```
187
188The problem, is that they didn't pick the **best** way to express the test's
189intent.
190
191By default, expectations don't have to be matched in *any* particular order. If
192you want them to match in a certain order, you need to be explicit. This is
193gMock's (and jMock's) fundamental philosophy: it's easy to accidentally
194over-specify your tests, and we want to make it harder to do so.
195
196There are two better ways to write the test spec. You could either put the
197expectations in sequence:
198
199```cpp
200using ::testing::Return;
201...
202  // foo.Bar() should be called twice, return 1 the first time, and return
203  // 2 the second time.  Using a sequence, we can write the expectations
204  // in their natural order.
205  {
206    InSequence s;
207    EXPECT_CALL(foo, Bar())
208        .WillOnce(Return(1))
209        .RetiresOnSaturation();
210    EXPECT_CALL(foo, Bar())
211        .WillOnce(Return(2))
212        .RetiresOnSaturation();
213  }
214```
215
216or you can put the sequence of actions in the same expectation:
217
218```cpp
219using ::testing::Return;
220...
221  // foo.Bar() should be called twice, return 1 the first time, and return
222  // 2 the second time.
223  EXPECT_CALL(foo, Bar())
224      .WillOnce(Return(1))
225      .WillOnce(Return(2))
226      .RetiresOnSaturation();
227```
228
229Back to the original questions: why does gMock search the expectations (and
230`ON_CALL`s) from back to front? Because this allows a user to set up a mock's
231behavior for the common case early (e.g. in the mock's constructor or the test
232fixture's set-up phase) and customize it with more specific rules later. If
233gMock searches from front to back, this very useful pattern won't be possible.
234
235### gMock prints a warning when a function without EXPECT_CALL is called, even if I have set its behavior using ON_CALL. Would it be reasonable not to show the warning in this case?
236
237When choosing between being neat and being safe, we lean toward the latter. So
238the answer is that we think it's better to show the warning.
239
240Often people write `ON_CALL`s in the mock object's constructor or `SetUp()`, as
241the default behavior rarely changes from test to test. Then in the test body
242they set the expectations, which are often different for each test. Having an
243`ON_CALL` in the set-up part of a test doesn't mean that the calls are expected.
244If there's no `EXPECT_CALL` and the method is called, it's possibly an error. If
245we quietly let the call go through without notifying the user, bugs may creep in
246unnoticed.
247
248If, however, you are sure that the calls are OK, you can write
249
250```cpp
251using ::testing::_;
252...
253  EXPECT_CALL(foo, Bar(_))
254      .WillRepeatedly(...);
255```
256
257instead of
258
259```cpp
260using ::testing::_;
261...
262  ON_CALL(foo, Bar(_))
263      .WillByDefault(...);
264```
265
266This tells gMock that you do expect the calls and no warning should be printed.
267
268Also, you can control the verbosity by specifying `--gmock_verbose=error`. Other
269values are `info` and `warning`. If you find the output too noisy when
270debugging, just choose a less verbose level.
271
272### How can I delete the mock function's argument in an action?
273
274If your mock function takes a pointer argument and you want to delete that
275argument, you can use testing::DeleteArg<N>() to delete the N'th (zero-indexed)
276argument:
277
278```cpp
279using ::testing::_;
280  ...
281  MOCK_METHOD(void, Bar, (X* x, const Y& y));
282  ...
283  EXPECT_CALL(mock_foo_, Bar(_, _))
284      .WillOnce(testing::DeleteArg<0>()));
285```
286
287### How can I perform an arbitrary action on a mock function's argument?
288
289If you find yourself needing to perform some action that's not supported by
290gMock directly, remember that you can define your own actions using
291[`MakeAction()`](#NewMonoActions) or
292[`MakePolymorphicAction()`](#NewPolyActions), or you can write a stub function
293and invoke it using [`Invoke()`](#FunctionsAsActions).
294
295```cpp
296using ::testing::_;
297using ::testing::Invoke;
298  ...
299  MOCK_METHOD(void, Bar, (X* p));
300  ...
301  EXPECT_CALL(mock_foo_, Bar(_))
302      .WillOnce(Invoke(MyAction(...)));
303```
304
305### My code calls a static/global function. Can I mock it?
306
307You can, but you need to make some changes.
308
309In general, if you find yourself needing to mock a static function, it's a sign
310that your modules are too tightly coupled (and less flexible, less reusable,
311less testable, etc). You are probably better off defining a small interface and
312call the function through that interface, which then can be easily mocked. It's
313a bit of work initially, but usually pays for itself quickly.
314
315This Google Testing Blog
316[post](https://testing.googleblog.com/2008/06/defeat-static-cling.html) says it
317excellently. Check it out.
318
319### My mock object needs to do complex stuff. It's a lot of pain to specify the actions. gMock sucks!
320
321I know it's not a question, but you get an answer for free any way. :-)
322
323With gMock, you can create mocks in C++ easily. And people might be tempted to
324use them everywhere. Sometimes they work great, and sometimes you may find them,
325well, a pain to use. So, what's wrong in the latter case?
326
327When you write a test without using mocks, you exercise the code and assert that
328it returns the correct value or that the system is in an expected state. This is
329sometimes called "state-based testing".
330
331Mocks are great for what some call "interaction-based" testing: instead of
332checking the system state at the very end, mock objects verify that they are
333invoked the right way and report an error as soon as it arises, giving you a
334handle on the precise context in which the error was triggered. This is often
335more effective and economical to do than state-based testing.
336
337If you are doing state-based testing and using a test double just to simulate
338the real object, you are probably better off using a fake. Using a mock in this
339case causes pain, as it's not a strong point for mocks to perform complex
340actions. If you experience this and think that mocks suck, you are just not
341using the right tool for your problem. Or, you might be trying to solve the
342wrong problem. :-)
343
344### I got a warning "Uninteresting function call encountered - default action taken.." Should I panic?
345
346By all means, NO! It's just an FYI. :-)
347
348What it means is that you have a mock function, you haven't set any expectations
349on it (by gMock's rule this means that you are not interested in calls to this
350function and therefore it can be called any number of times), and it is called.
351That's OK - you didn't say it's not OK to call the function!
352
353What if you actually meant to disallow this function to be called, but forgot to
354write `EXPECT_CALL(foo, Bar()).Times(0)`? While one can argue that it's the
355user's fault, gMock tries to be nice and prints you a note.
356
357So, when you see the message and believe that there shouldn't be any
358uninteresting calls, you should investigate what's going on. To make your life
359easier, gMock dumps the stack trace when an uninteresting call is encountered.
360From that you can figure out which mock function it is, and how it is called.
361
362### I want to define a custom action. Should I use Invoke() or implement the ActionInterface interface?
363
364Either way is fine - you want to choose the one that's more convenient for your
365circumstance.
366
367Usually, if your action is for a particular function type, defining it using
368`Invoke()` should be easier; if your action can be used in functions of
369different types (e.g. if you are defining `Return(*value*)`),
370`MakePolymorphicAction()` is easiest. Sometimes you want precise control on what
371types of functions the action can be used in, and implementing `ActionInterface`
372is the way to go here. See the implementation of `Return()` in `gmock-actions.h`
373for an example.
374
375### I use SetArgPointee() in WillOnce(), but gcc complains about "conflicting return type specified". What does it mean?
376
377You got this error as gMock has no idea what value it should return when the
378mock method is called. `SetArgPointee()` says what the side effect is, but
379doesn't say what the return value should be. You need `DoAll()` to chain a
380`SetArgPointee()` with a `Return()` that provides a value appropriate to the API
381being mocked.
382
383See this [recipe](gmock_cook_book.md#mocking-side-effects) for more details and
384an example.
385
386### I have a huge mock class, and Microsoft Visual C++ runs out of memory when compiling it. What can I do?
387
388We've noticed that when the `/clr` compiler flag is used, Visual C++ uses 5~6
389times as much memory when compiling a mock class. We suggest to avoid `/clr`
390when compiling native C++ mocks.
391