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