You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

390 lines
15 KiB

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