1f92157deSopenharmony_ci# Mocking Reference
2f92157deSopenharmony_ci
3f92157deSopenharmony_ciThis page lists the facilities provided by GoogleTest for creating and working
4f92157deSopenharmony_ciwith mock objects. To use them, include the header
5f92157deSopenharmony_ci`gmock/gmock.h`.
6f92157deSopenharmony_ci
7f92157deSopenharmony_ci## Macros {#macros}
8f92157deSopenharmony_ci
9f92157deSopenharmony_ciGoogleTest defines the following macros for working with mocks.
10f92157deSopenharmony_ci
11f92157deSopenharmony_ci### MOCK_METHOD {#MOCK_METHOD}
12f92157deSopenharmony_ci
13f92157deSopenharmony_ci`MOCK_METHOD(`*`return_type`*`,`*`method_name`*`, (`*`args...`*`));` \
14f92157deSopenharmony_ci`MOCK_METHOD(`*`return_type`*`,`*`method_name`*`, (`*`args...`*`),
15f92157deSopenharmony_ci(`*`specs...`*`));`
16f92157deSopenharmony_ci
17f92157deSopenharmony_ciDefines a mock method *`method_name`* with arguments `(`*`args...`*`)` and
18f92157deSopenharmony_cireturn type *`return_type`* within a mock class.
19f92157deSopenharmony_ci
20f92157deSopenharmony_ciThe parameters of `MOCK_METHOD` mirror the method declaration. The optional
21f92157deSopenharmony_cifourth parameter *`specs...`* is a comma-separated list of qualifiers. The
22f92157deSopenharmony_cifollowing qualifiers are accepted:
23f92157deSopenharmony_ci
24f92157deSopenharmony_ci| Qualifier                  | Meaning                                      |
25f92157deSopenharmony_ci| -------------------------- | -------------------------------------------- |
26f92157deSopenharmony_ci| `const`                    | Makes the mocked method a `const` method. Required if overriding a `const` method. |
27f92157deSopenharmony_ci| `override`                 | Marks the method with `override`. Recommended if overriding a `virtual` method. |
28f92157deSopenharmony_ci| `noexcept`                 | Marks the method with `noexcept`. Required if overriding a `noexcept` method. |
29f92157deSopenharmony_ci| `Calltype(`*`calltype`*`)` | Sets the call type for the method, for example `Calltype(STDMETHODCALLTYPE)`. Useful on Windows. |
30f92157deSopenharmony_ci| `ref(`*`qualifier`*`)`     | Marks the method with the given reference qualifier, for example `ref(&)` or `ref(&&)`. Required if overriding a method that has a reference qualifier. |
31f92157deSopenharmony_ci
32f92157deSopenharmony_ciNote that commas in arguments prevent `MOCK_METHOD` from parsing the arguments
33f92157deSopenharmony_cicorrectly if they are not appropriately surrounded by parentheses. See the
34f92157deSopenharmony_cifollowing example:
35f92157deSopenharmony_ci
36f92157deSopenharmony_ci```cpp
37f92157deSopenharmony_ciclass MyMock {
38f92157deSopenharmony_ci public:
39f92157deSopenharmony_ci  // The following 2 lines will not compile due to commas in the arguments:
40f92157deSopenharmony_ci  MOCK_METHOD(std::pair<bool, int>, GetPair, ());              // Error!
41f92157deSopenharmony_ci  MOCK_METHOD(bool, CheckMap, (std::map<int, double>, bool));  // Error!
42f92157deSopenharmony_ci
43f92157deSopenharmony_ci  // One solution - wrap arguments that contain commas in parentheses:
44f92157deSopenharmony_ci  MOCK_METHOD((std::pair<bool, int>), GetPair, ());
45f92157deSopenharmony_ci  MOCK_METHOD(bool, CheckMap, ((std::map<int, double>), bool));
46f92157deSopenharmony_ci
47f92157deSopenharmony_ci  // Another solution - use type aliases:
48f92157deSopenharmony_ci  using BoolAndInt = std::pair<bool, int>;
49f92157deSopenharmony_ci  MOCK_METHOD(BoolAndInt, GetPair, ());
50f92157deSopenharmony_ci  using MapIntDouble = std::map<int, double>;
51f92157deSopenharmony_ci  MOCK_METHOD(bool, CheckMap, (MapIntDouble, bool));
52f92157deSopenharmony_ci};
53f92157deSopenharmony_ci```
54f92157deSopenharmony_ci
55f92157deSopenharmony_ci`MOCK_METHOD` must be used in the `public:` section of a mock class definition,
56f92157deSopenharmony_ciregardless of whether the method being mocked is `public`, `protected`, or
57f92157deSopenharmony_ci`private` in the base class.
58f92157deSopenharmony_ci
59f92157deSopenharmony_ci### EXPECT_CALL {#EXPECT_CALL}
60f92157deSopenharmony_ci
61f92157deSopenharmony_ci`EXPECT_CALL(`*`mock_object`*`,`*`method_name`*`(`*`matchers...`*`))`
62f92157deSopenharmony_ci
63f92157deSopenharmony_ciCreates an [expectation](../gmock_for_dummies.md#setting-expectations) that the
64f92157deSopenharmony_cimethod *`method_name`* of the object *`mock_object`* is called with arguments
65f92157deSopenharmony_cithat match the given matchers *`matchers...`*. `EXPECT_CALL` must precede any
66f92157deSopenharmony_cicode that exercises the mock object.
67f92157deSopenharmony_ci
68f92157deSopenharmony_ciThe parameter *`matchers...`* is a comma-separated list of
69f92157deSopenharmony_ci[matchers](../gmock_for_dummies.md#matchers-what-arguments-do-we-expect) that
70f92157deSopenharmony_cicorrespond to each argument of the method *`method_name`*. The expectation will
71f92157deSopenharmony_ciapply only to calls of *`method_name`* whose arguments match all of the
72f92157deSopenharmony_cimatchers. If `(`*`matchers...`*`)` is omitted, the expectation behaves as if
73f92157deSopenharmony_cieach argument's matcher were a [wildcard matcher (`_`)](matchers.md#wildcard).
74f92157deSopenharmony_ciSee the [Matchers Reference](matchers.md) for a list of all built-in matchers.
75f92157deSopenharmony_ci
76f92157deSopenharmony_ciThe following chainable clauses can be used to modify the expectation, and they
77f92157deSopenharmony_cimust be used in the following order:
78f92157deSopenharmony_ci
79f92157deSopenharmony_ci```cpp
80f92157deSopenharmony_ciEXPECT_CALL(mock_object, method_name(matchers...))
81f92157deSopenharmony_ci    .With(multi_argument_matcher)  // Can be used at most once
82f92157deSopenharmony_ci    .Times(cardinality)            // Can be used at most once
83f92157deSopenharmony_ci    .InSequence(sequences...)      // Can be used any number of times
84f92157deSopenharmony_ci    .After(expectations...)        // Can be used any number of times
85f92157deSopenharmony_ci    .WillOnce(action)              // Can be used any number of times
86f92157deSopenharmony_ci    .WillRepeatedly(action)        // Can be used at most once
87f92157deSopenharmony_ci    .RetiresOnSaturation();        // Can be used at most once
88f92157deSopenharmony_ci```
89f92157deSopenharmony_ci
90f92157deSopenharmony_ciSee details for each modifier clause below.
91f92157deSopenharmony_ci
92f92157deSopenharmony_ci#### With {#EXPECT_CALL.With}
93f92157deSopenharmony_ci
94f92157deSopenharmony_ci`.With(`*`multi_argument_matcher`*`)`
95f92157deSopenharmony_ci
96f92157deSopenharmony_ciRestricts the expectation to apply only to mock function calls whose arguments
97f92157deSopenharmony_cias a whole match the multi-argument matcher *`multi_argument_matcher`*.
98f92157deSopenharmony_ci
99f92157deSopenharmony_ciGoogleTest passes all of the arguments as one tuple into the matcher. The
100f92157deSopenharmony_ciparameter *`multi_argument_matcher`* must thus be a matcher of type
101f92157deSopenharmony_ci`Matcher<std::tuple<A1, ..., An>>`, where `A1, ..., An` are the types of the
102f92157deSopenharmony_cifunction arguments.
103f92157deSopenharmony_ci
104f92157deSopenharmony_ciFor example, the following code sets the expectation that
105f92157deSopenharmony_ci`my_mock.SetPosition()` is called with any two arguments, the first argument
106f92157deSopenharmony_cibeing less than the second:
107f92157deSopenharmony_ci
108f92157deSopenharmony_ci```cpp
109f92157deSopenharmony_ciusing ::testing::_;
110f92157deSopenharmony_ciusing ::testing::Lt;
111f92157deSopenharmony_ci...
112f92157deSopenharmony_ciEXPECT_CALL(my_mock, SetPosition(_, _))
113f92157deSopenharmony_ci    .With(Lt());
114f92157deSopenharmony_ci```
115f92157deSopenharmony_ci
116f92157deSopenharmony_ciGoogleTest provides some built-in matchers for 2-tuples, including the `Lt()`
117f92157deSopenharmony_cimatcher above. See [Multi-argument Matchers](matchers.md#MultiArgMatchers).
118f92157deSopenharmony_ci
119f92157deSopenharmony_ciThe `With` clause can be used at most once on an expectation and must be the
120f92157deSopenharmony_cifirst clause.
121f92157deSopenharmony_ci
122f92157deSopenharmony_ci#### Times {#EXPECT_CALL.Times}
123f92157deSopenharmony_ci
124f92157deSopenharmony_ci`.Times(`*`cardinality`*`)`
125f92157deSopenharmony_ci
126f92157deSopenharmony_ciSpecifies how many times the mock function call is expected.
127f92157deSopenharmony_ci
128f92157deSopenharmony_ciThe parameter *`cardinality`* represents the number of expected calls and can be
129f92157deSopenharmony_cione of the following, all defined in the `::testing` namespace:
130f92157deSopenharmony_ci
131f92157deSopenharmony_ci| Cardinality         | Meaning                                             |
132f92157deSopenharmony_ci| ------------------- | --------------------------------------------------- |
133f92157deSopenharmony_ci| `AnyNumber()`       | The function can be called any number of times.     |
134f92157deSopenharmony_ci| `AtLeast(n)`        | The function call is expected at least *n* times.   |
135f92157deSopenharmony_ci| `AtMost(n)`         | The function call is expected at most *n* times.    |
136f92157deSopenharmony_ci| `Between(m, n)`     | The function call is expected between *m* and *n* times, inclusive. |
137f92157deSopenharmony_ci| `Exactly(n)` or `n` | The function call is expected exactly *n* times. If *n* is 0, the call should never happen. |
138f92157deSopenharmony_ci
139f92157deSopenharmony_ciIf the `Times` clause is omitted, GoogleTest infers the cardinality as follows:
140f92157deSopenharmony_ci
141f92157deSopenharmony_ci*   If neither [`WillOnce`](#EXPECT_CALL.WillOnce) nor
142f92157deSopenharmony_ci    [`WillRepeatedly`](#EXPECT_CALL.WillRepeatedly) are specified, the inferred
143f92157deSopenharmony_ci    cardinality is `Times(1)`.
144f92157deSopenharmony_ci*   If there are *n* `WillOnce` clauses and no `WillRepeatedly` clause, where
145f92157deSopenharmony_ci    *n* >= 1, the inferred cardinality is `Times(n)`.
146f92157deSopenharmony_ci*   If there are *n* `WillOnce` clauses and one `WillRepeatedly` clause, where
147f92157deSopenharmony_ci    *n* >= 0, the inferred cardinality is `Times(AtLeast(n))`.
148f92157deSopenharmony_ci
149f92157deSopenharmony_ciThe `Times` clause can be used at most once on an expectation.
150f92157deSopenharmony_ci
151f92157deSopenharmony_ci#### InSequence {#EXPECT_CALL.InSequence}
152f92157deSopenharmony_ci
153f92157deSopenharmony_ci`.InSequence(`*`sequences...`*`)`
154f92157deSopenharmony_ci
155f92157deSopenharmony_ciSpecifies that the mock function call is expected in a certain sequence.
156f92157deSopenharmony_ci
157f92157deSopenharmony_ciThe parameter *`sequences...`* is any number of [`Sequence`](#Sequence) objects.
158f92157deSopenharmony_ciExpected calls assigned to the same sequence are expected to occur in the order
159f92157deSopenharmony_cithe expectations are declared.
160f92157deSopenharmony_ci
161f92157deSopenharmony_ciFor example, the following code sets the expectation that the `Reset()` method
162f92157deSopenharmony_ciof `my_mock` is called before both `GetSize()` and `Describe()`, and `GetSize()`
163f92157deSopenharmony_ciand `Describe()` can occur in any order relative to each other:
164f92157deSopenharmony_ci
165f92157deSopenharmony_ci```cpp
166f92157deSopenharmony_ciusing ::testing::Sequence;
167f92157deSopenharmony_ciSequence s1, s2;
168f92157deSopenharmony_ci...
169f92157deSopenharmony_ciEXPECT_CALL(my_mock, Reset())
170f92157deSopenharmony_ci    .InSequence(s1, s2);
171f92157deSopenharmony_ciEXPECT_CALL(my_mock, GetSize())
172f92157deSopenharmony_ci    .InSequence(s1);
173f92157deSopenharmony_ciEXPECT_CALL(my_mock, Describe())
174f92157deSopenharmony_ci    .InSequence(s2);
175f92157deSopenharmony_ci```
176f92157deSopenharmony_ci
177f92157deSopenharmony_ciThe `InSequence` clause can be used any number of times on an expectation.
178f92157deSopenharmony_ci
179f92157deSopenharmony_ciSee also the [`InSequence` class](#InSequence).
180f92157deSopenharmony_ci
181f92157deSopenharmony_ci#### After {#EXPECT_CALL.After}
182f92157deSopenharmony_ci
183f92157deSopenharmony_ci`.After(`*`expectations...`*`)`
184f92157deSopenharmony_ci
185f92157deSopenharmony_ciSpecifies that the mock function call is expected to occur after one or more
186f92157deSopenharmony_ciother calls.
187f92157deSopenharmony_ci
188f92157deSopenharmony_ciThe parameter *`expectations...`* can be up to five
189f92157deSopenharmony_ci[`Expectation`](#Expectation) or [`ExpectationSet`](#ExpectationSet) objects.
190f92157deSopenharmony_ciThe mock function call is expected to occur after all of the given expectations.
191f92157deSopenharmony_ci
192f92157deSopenharmony_ciFor example, the following code sets the expectation that the `Describe()`
193f92157deSopenharmony_cimethod of `my_mock` is called only after both `InitX()` and `InitY()` have been
194f92157deSopenharmony_cicalled.
195f92157deSopenharmony_ci
196f92157deSopenharmony_ci```cpp
197f92157deSopenharmony_ciusing ::testing::Expectation;
198f92157deSopenharmony_ci...
199f92157deSopenharmony_ciExpectation init_x = EXPECT_CALL(my_mock, InitX());
200f92157deSopenharmony_ciExpectation init_y = EXPECT_CALL(my_mock, InitY());
201f92157deSopenharmony_ciEXPECT_CALL(my_mock, Describe())
202f92157deSopenharmony_ci    .After(init_x, init_y);
203f92157deSopenharmony_ci```
204f92157deSopenharmony_ci
205f92157deSopenharmony_ciThe `ExpectationSet` object is helpful when the number of prerequisites for an
206f92157deSopenharmony_ciexpectation is large or variable, for example:
207f92157deSopenharmony_ci
208f92157deSopenharmony_ci```cpp
209f92157deSopenharmony_ciusing ::testing::ExpectationSet;
210f92157deSopenharmony_ci...
211f92157deSopenharmony_ciExpectationSet all_inits;
212f92157deSopenharmony_ci// Collect all expectations of InitElement() calls
213f92157deSopenharmony_cifor (int i = 0; i < element_count; i++) {
214f92157deSopenharmony_ci  all_inits += EXPECT_CALL(my_mock, InitElement(i));
215f92157deSopenharmony_ci}
216f92157deSopenharmony_ciEXPECT_CALL(my_mock, Describe())
217f92157deSopenharmony_ci    .After(all_inits);  // Expect Describe() call after all InitElement() calls
218f92157deSopenharmony_ci```
219f92157deSopenharmony_ci
220f92157deSopenharmony_ciThe `After` clause can be used any number of times on an expectation.
221f92157deSopenharmony_ci
222f92157deSopenharmony_ci#### WillOnce {#EXPECT_CALL.WillOnce}
223f92157deSopenharmony_ci
224f92157deSopenharmony_ci`.WillOnce(`*`action`*`)`
225f92157deSopenharmony_ci
226f92157deSopenharmony_ciSpecifies the mock function's actual behavior when invoked, for a single
227f92157deSopenharmony_cimatching function call.
228f92157deSopenharmony_ci
229f92157deSopenharmony_ciThe parameter *`action`* represents the
230f92157deSopenharmony_ci[action](../gmock_for_dummies.md#actions-what-should-it-do) that the function
231f92157deSopenharmony_cicall will perform. See the [Actions Reference](actions.md) for a list of
232f92157deSopenharmony_cibuilt-in actions.
233f92157deSopenharmony_ci
234f92157deSopenharmony_ciThe use of `WillOnce` implicitly sets a cardinality on the expectation when
235f92157deSopenharmony_ci`Times` is not specified. See [`Times`](#EXPECT_CALL.Times).
236f92157deSopenharmony_ci
237f92157deSopenharmony_ciEach matching function call will perform the next action in the order declared.
238f92157deSopenharmony_ciFor example, the following code specifies that `my_mock.GetNumber()` is expected
239f92157deSopenharmony_cito be called exactly 3 times and will return `1`, `2`, and `3` respectively on
240f92157deSopenharmony_cithe first, second, and third calls:
241f92157deSopenharmony_ci
242f92157deSopenharmony_ci```cpp
243f92157deSopenharmony_ciusing ::testing::Return;
244f92157deSopenharmony_ci...
245f92157deSopenharmony_ciEXPECT_CALL(my_mock, GetNumber())
246f92157deSopenharmony_ci    .WillOnce(Return(1))
247f92157deSopenharmony_ci    .WillOnce(Return(2))
248f92157deSopenharmony_ci    .WillOnce(Return(3));
249f92157deSopenharmony_ci```
250f92157deSopenharmony_ci
251f92157deSopenharmony_ciThe `WillOnce` clause can be used any number of times on an expectation. Unlike
252f92157deSopenharmony_ci`WillRepeatedly`, the action fed to each `WillOnce` call will be called at most
253f92157deSopenharmony_cionce, so may be a move-only type and/or have an `&&`-qualified call operator.
254f92157deSopenharmony_ci
255f92157deSopenharmony_ci#### WillRepeatedly {#EXPECT_CALL.WillRepeatedly}
256f92157deSopenharmony_ci
257f92157deSopenharmony_ci`.WillRepeatedly(`*`action`*`)`
258f92157deSopenharmony_ci
259f92157deSopenharmony_ciSpecifies the mock function's actual behavior when invoked, for all subsequent
260f92157deSopenharmony_cimatching function calls. Takes effect after the actions specified in the
261f92157deSopenharmony_ci[`WillOnce`](#EXPECT_CALL.WillOnce) clauses, if any, have been performed.
262f92157deSopenharmony_ci
263f92157deSopenharmony_ciThe parameter *`action`* represents the
264f92157deSopenharmony_ci[action](../gmock_for_dummies.md#actions-what-should-it-do) that the function
265f92157deSopenharmony_cicall will perform. See the [Actions Reference](actions.md) for a list of
266f92157deSopenharmony_cibuilt-in actions.
267f92157deSopenharmony_ci
268f92157deSopenharmony_ciThe use of `WillRepeatedly` implicitly sets a cardinality on the expectation
269f92157deSopenharmony_ciwhen `Times` is not specified. See [`Times`](#EXPECT_CALL.Times).
270f92157deSopenharmony_ci
271f92157deSopenharmony_ciIf any `WillOnce` clauses have been specified, matching function calls will
272f92157deSopenharmony_ciperform those actions before the action specified by `WillRepeatedly`. See the
273f92157deSopenharmony_cifollowing example:
274f92157deSopenharmony_ci
275f92157deSopenharmony_ci```cpp
276f92157deSopenharmony_ciusing ::testing::Return;
277f92157deSopenharmony_ci...
278f92157deSopenharmony_ciEXPECT_CALL(my_mock, GetName())
279f92157deSopenharmony_ci    .WillRepeatedly(Return("John Doe"));  // Return "John Doe" on all calls
280f92157deSopenharmony_ci
281f92157deSopenharmony_ciEXPECT_CALL(my_mock, GetNumber())
282f92157deSopenharmony_ci    .WillOnce(Return(42))        // Return 42 on the first call
283f92157deSopenharmony_ci    .WillRepeatedly(Return(7));  // Return 7 on all subsequent calls
284f92157deSopenharmony_ci```
285f92157deSopenharmony_ci
286f92157deSopenharmony_ciThe `WillRepeatedly` clause can be used at most once on an expectation.
287f92157deSopenharmony_ci
288f92157deSopenharmony_ci#### RetiresOnSaturation {#EXPECT_CALL.RetiresOnSaturation}
289f92157deSopenharmony_ci
290f92157deSopenharmony_ci`.RetiresOnSaturation()`
291f92157deSopenharmony_ci
292f92157deSopenharmony_ciIndicates that the expectation will no longer be active after the expected
293f92157deSopenharmony_cinumber of matching function calls has been reached.
294f92157deSopenharmony_ci
295f92157deSopenharmony_ciThe `RetiresOnSaturation` clause is only meaningful for expectations with an
296f92157deSopenharmony_ciupper-bounded cardinality. The expectation will *retire* (no longer match any
297f92157deSopenharmony_cifunction calls) after it has been *saturated* (the upper bound has been
298f92157deSopenharmony_cireached). See the following example:
299f92157deSopenharmony_ci
300f92157deSopenharmony_ci```cpp
301f92157deSopenharmony_ciusing ::testing::_;
302f92157deSopenharmony_ciusing ::testing::AnyNumber;
303f92157deSopenharmony_ci...
304f92157deSopenharmony_ciEXPECT_CALL(my_mock, SetNumber(_))  // Expectation 1
305f92157deSopenharmony_ci    .Times(AnyNumber());
306f92157deSopenharmony_ciEXPECT_CALL(my_mock, SetNumber(7))  // Expectation 2
307f92157deSopenharmony_ci    .Times(2)
308f92157deSopenharmony_ci    .RetiresOnSaturation();
309f92157deSopenharmony_ci```
310f92157deSopenharmony_ci
311f92157deSopenharmony_ciIn the above example, the first two calls to `my_mock.SetNumber(7)` match
312f92157deSopenharmony_ciexpectation 2, which then becomes inactive and no longer matches any calls. A
313f92157deSopenharmony_cithird call to `my_mock.SetNumber(7)` would then match expectation 1. Without
314f92157deSopenharmony_ci`RetiresOnSaturation()` on expectation 2, a third call to `my_mock.SetNumber(7)`
315f92157deSopenharmony_ciwould match expectation 2 again, producing a failure since the limit of 2 calls
316f92157deSopenharmony_ciwas exceeded.
317f92157deSopenharmony_ci
318f92157deSopenharmony_ciThe `RetiresOnSaturation` clause can be used at most once on an expectation and
319f92157deSopenharmony_cimust be the last clause.
320f92157deSopenharmony_ci
321f92157deSopenharmony_ci### ON_CALL {#ON_CALL}
322f92157deSopenharmony_ci
323f92157deSopenharmony_ci`ON_CALL(`*`mock_object`*`,`*`method_name`*`(`*`matchers...`*`))`
324f92157deSopenharmony_ci
325f92157deSopenharmony_ciDefines what happens when the method *`method_name`* of the object
326f92157deSopenharmony_ci*`mock_object`* is called with arguments that match the given matchers
327f92157deSopenharmony_ci*`matchers...`*. Requires a modifier clause to specify the method's behavior.
328f92157deSopenharmony_ci*Does not* set any expectations that the method will be called.
329f92157deSopenharmony_ci
330f92157deSopenharmony_ciThe parameter *`matchers...`* is a comma-separated list of
331f92157deSopenharmony_ci[matchers](../gmock_for_dummies.md#matchers-what-arguments-do-we-expect) that
332f92157deSopenharmony_cicorrespond to each argument of the method *`method_name`*. The `ON_CALL`
333f92157deSopenharmony_cispecification will apply only to calls of *`method_name`* whose arguments match
334f92157deSopenharmony_ciall of the matchers. If `(`*`matchers...`*`)` is omitted, the behavior is as if
335f92157deSopenharmony_cieach argument's matcher were a [wildcard matcher (`_`)](matchers.md#wildcard).
336f92157deSopenharmony_ciSee the [Matchers Reference](matchers.md) for a list of all built-in matchers.
337f92157deSopenharmony_ci
338f92157deSopenharmony_ciThe following chainable clauses can be used to set the method's behavior, and
339f92157deSopenharmony_cithey must be used in the following order:
340f92157deSopenharmony_ci
341f92157deSopenharmony_ci```cpp
342f92157deSopenharmony_ciON_CALL(mock_object, method_name(matchers...))
343f92157deSopenharmony_ci    .With(multi_argument_matcher)  // Can be used at most once
344f92157deSopenharmony_ci    .WillByDefault(action);        // Required
345f92157deSopenharmony_ci```
346f92157deSopenharmony_ci
347f92157deSopenharmony_ciSee details for each modifier clause below.
348f92157deSopenharmony_ci
349f92157deSopenharmony_ci#### With {#ON_CALL.With}
350f92157deSopenharmony_ci
351f92157deSopenharmony_ci`.With(`*`multi_argument_matcher`*`)`
352f92157deSopenharmony_ci
353f92157deSopenharmony_ciRestricts the specification to only mock function calls whose arguments as a
354f92157deSopenharmony_ciwhole match the multi-argument matcher *`multi_argument_matcher`*.
355f92157deSopenharmony_ci
356f92157deSopenharmony_ciGoogleTest passes all of the arguments as one tuple into the matcher. The
357f92157deSopenharmony_ciparameter *`multi_argument_matcher`* must thus be a matcher of type
358f92157deSopenharmony_ci`Matcher<std::tuple<A1, ..., An>>`, where `A1, ..., An` are the types of the
359f92157deSopenharmony_cifunction arguments.
360f92157deSopenharmony_ci
361f92157deSopenharmony_ciFor example, the following code sets the default behavior when
362f92157deSopenharmony_ci`my_mock.SetPosition()` is called with any two arguments, the first argument
363f92157deSopenharmony_cibeing less than the second:
364f92157deSopenharmony_ci
365f92157deSopenharmony_ci```cpp
366f92157deSopenharmony_ciusing ::testing::_;
367f92157deSopenharmony_ciusing ::testing::Lt;
368f92157deSopenharmony_ciusing ::testing::Return;
369f92157deSopenharmony_ci...
370f92157deSopenharmony_ciON_CALL(my_mock, SetPosition(_, _))
371f92157deSopenharmony_ci    .With(Lt())
372f92157deSopenharmony_ci    .WillByDefault(Return(true));
373f92157deSopenharmony_ci```
374f92157deSopenharmony_ci
375f92157deSopenharmony_ciGoogleTest provides some built-in matchers for 2-tuples, including the `Lt()`
376f92157deSopenharmony_cimatcher above. See [Multi-argument Matchers](matchers.md#MultiArgMatchers).
377f92157deSopenharmony_ci
378f92157deSopenharmony_ciThe `With` clause can be used at most once with each `ON_CALL` statement.
379f92157deSopenharmony_ci
380f92157deSopenharmony_ci#### WillByDefault {#ON_CALL.WillByDefault}
381f92157deSopenharmony_ci
382f92157deSopenharmony_ci`.WillByDefault(`*`action`*`)`
383f92157deSopenharmony_ci
384f92157deSopenharmony_ciSpecifies the default behavior of a matching mock function call.
385f92157deSopenharmony_ci
386f92157deSopenharmony_ciThe parameter *`action`* represents the
387f92157deSopenharmony_ci[action](../gmock_for_dummies.md#actions-what-should-it-do) that the function
388f92157deSopenharmony_cicall will perform. See the [Actions Reference](actions.md) for a list of
389f92157deSopenharmony_cibuilt-in actions.
390f92157deSopenharmony_ci
391f92157deSopenharmony_ciFor example, the following code specifies that by default, a call to
392f92157deSopenharmony_ci`my_mock.Greet()` will return `"hello"`:
393f92157deSopenharmony_ci
394f92157deSopenharmony_ci```cpp
395f92157deSopenharmony_ciusing ::testing::Return;
396f92157deSopenharmony_ci...
397f92157deSopenharmony_ciON_CALL(my_mock, Greet())
398f92157deSopenharmony_ci    .WillByDefault(Return("hello"));
399f92157deSopenharmony_ci```
400f92157deSopenharmony_ci
401f92157deSopenharmony_ciThe action specified by `WillByDefault` is superseded by the actions specified
402f92157deSopenharmony_cion a matching `EXPECT_CALL` statement, if any. See the
403f92157deSopenharmony_ci[`WillOnce`](#EXPECT_CALL.WillOnce) and
404f92157deSopenharmony_ci[`WillRepeatedly`](#EXPECT_CALL.WillRepeatedly) clauses of `EXPECT_CALL`.
405f92157deSopenharmony_ci
406f92157deSopenharmony_ciThe `WillByDefault` clause must be used exactly once with each `ON_CALL`
407f92157deSopenharmony_cistatement.
408f92157deSopenharmony_ci
409f92157deSopenharmony_ci## Classes {#classes}
410f92157deSopenharmony_ci
411f92157deSopenharmony_ciGoogleTest defines the following classes for working with mocks.
412f92157deSopenharmony_ci
413f92157deSopenharmony_ci### DefaultValue {#DefaultValue}
414f92157deSopenharmony_ci
415f92157deSopenharmony_ci`::testing::DefaultValue<T>`
416f92157deSopenharmony_ci
417f92157deSopenharmony_ciAllows a user to specify the default value for a type `T` that is both copyable
418f92157deSopenharmony_ciand publicly destructible (i.e. anything that can be used as a function return
419f92157deSopenharmony_citype). For mock functions with a return type of `T`, this default value is
420f92157deSopenharmony_cireturned from function calls that do not specify an action.
421f92157deSopenharmony_ci
422f92157deSopenharmony_ciProvides the static methods `Set()`, `SetFactory()`, and `Clear()` to manage the
423f92157deSopenharmony_cidefault value:
424f92157deSopenharmony_ci
425f92157deSopenharmony_ci```cpp
426f92157deSopenharmony_ci// Sets the default value to be returned. T must be copy constructible.
427f92157deSopenharmony_ciDefaultValue<T>::Set(value);
428f92157deSopenharmony_ci
429f92157deSopenharmony_ci// Sets a factory. Will be invoked on demand. T must be move constructible.
430f92157deSopenharmony_ciT MakeT();
431f92157deSopenharmony_ciDefaultValue<T>::SetFactory(&MakeT);
432f92157deSopenharmony_ci
433f92157deSopenharmony_ci// Unsets the default value.
434f92157deSopenharmony_ciDefaultValue<T>::Clear();
435f92157deSopenharmony_ci```
436f92157deSopenharmony_ci
437f92157deSopenharmony_ci### NiceMock {#NiceMock}
438f92157deSopenharmony_ci
439f92157deSopenharmony_ci`::testing::NiceMock<T>`
440f92157deSopenharmony_ci
441f92157deSopenharmony_ciRepresents a mock object that suppresses warnings on
442f92157deSopenharmony_ci[uninteresting calls](../gmock_cook_book.md#uninteresting-vs-unexpected). The
443f92157deSopenharmony_citemplate parameter `T` is any mock class, except for another `NiceMock`,
444f92157deSopenharmony_ci`NaggyMock`, or `StrictMock`.
445f92157deSopenharmony_ci
446f92157deSopenharmony_ciUsage of `NiceMock<T>` is analogous to usage of `T`. `NiceMock<T>` is a subclass
447f92157deSopenharmony_ciof `T`, so it can be used wherever an object of type `T` is accepted. In
448f92157deSopenharmony_ciaddition, `NiceMock<T>` can be constructed with any arguments that a constructor
449f92157deSopenharmony_ciof `T` accepts.
450f92157deSopenharmony_ci
451f92157deSopenharmony_ciFor example, the following code suppresses warnings on the mock `my_mock` of
452f92157deSopenharmony_citype `MockClass` if a method other than `DoSomething()` is called:
453f92157deSopenharmony_ci
454f92157deSopenharmony_ci```cpp
455f92157deSopenharmony_ciusing ::testing::NiceMock;
456f92157deSopenharmony_ci...
457f92157deSopenharmony_ciNiceMock<MockClass> my_mock("some", "args");
458f92157deSopenharmony_ciEXPECT_CALL(my_mock, DoSomething());
459f92157deSopenharmony_ci... code that uses my_mock ...
460f92157deSopenharmony_ci```
461f92157deSopenharmony_ci
462f92157deSopenharmony_ci`NiceMock<T>` only works for mock methods defined using the `MOCK_METHOD` macro
463f92157deSopenharmony_cidirectly in the definition of class `T`. If a mock method is defined in a base
464f92157deSopenharmony_ciclass of `T`, a warning might still be generated.
465f92157deSopenharmony_ci
466f92157deSopenharmony_ci`NiceMock<T>` might not work correctly if the destructor of `T` is not virtual.
467f92157deSopenharmony_ci
468f92157deSopenharmony_ci### NaggyMock {#NaggyMock}
469f92157deSopenharmony_ci
470f92157deSopenharmony_ci`::testing::NaggyMock<T>`
471f92157deSopenharmony_ci
472f92157deSopenharmony_ciRepresents a mock object that generates warnings on
473f92157deSopenharmony_ci[uninteresting calls](../gmock_cook_book.md#uninteresting-vs-unexpected). The
474f92157deSopenharmony_citemplate parameter `T` is any mock class, except for another `NiceMock`,
475f92157deSopenharmony_ci`NaggyMock`, or `StrictMock`.
476f92157deSopenharmony_ci
477f92157deSopenharmony_ciUsage of `NaggyMock<T>` is analogous to usage of `T`. `NaggyMock<T>` is a
478f92157deSopenharmony_cisubclass of `T`, so it can be used wherever an object of type `T` is accepted.
479f92157deSopenharmony_ciIn addition, `NaggyMock<T>` can be constructed with any arguments that a
480f92157deSopenharmony_ciconstructor of `T` accepts.
481f92157deSopenharmony_ci
482f92157deSopenharmony_ciFor example, the following code generates warnings on the mock `my_mock` of type
483f92157deSopenharmony_ci`MockClass` if a method other than `DoSomething()` is called:
484f92157deSopenharmony_ci
485f92157deSopenharmony_ci```cpp
486f92157deSopenharmony_ciusing ::testing::NaggyMock;
487f92157deSopenharmony_ci...
488f92157deSopenharmony_ciNaggyMock<MockClass> my_mock("some", "args");
489f92157deSopenharmony_ciEXPECT_CALL(my_mock, DoSomething());
490f92157deSopenharmony_ci... code that uses my_mock ...
491f92157deSopenharmony_ci```
492f92157deSopenharmony_ci
493f92157deSopenharmony_ciMock objects of type `T` by default behave the same way as `NaggyMock<T>`.
494f92157deSopenharmony_ci
495f92157deSopenharmony_ci### StrictMock {#StrictMock}
496f92157deSopenharmony_ci
497f92157deSopenharmony_ci`::testing::StrictMock<T>`
498f92157deSopenharmony_ci
499f92157deSopenharmony_ciRepresents a mock object that generates test failures on
500f92157deSopenharmony_ci[uninteresting calls](../gmock_cook_book.md#uninteresting-vs-unexpected). The
501f92157deSopenharmony_citemplate parameter `T` is any mock class, except for another `NiceMock`,
502f92157deSopenharmony_ci`NaggyMock`, or `StrictMock`.
503f92157deSopenharmony_ci
504f92157deSopenharmony_ciUsage of `StrictMock<T>` is analogous to usage of `T`. `StrictMock<T>` is a
505f92157deSopenharmony_cisubclass of `T`, so it can be used wherever an object of type `T` is accepted.
506f92157deSopenharmony_ciIn addition, `StrictMock<T>` can be constructed with any arguments that a
507f92157deSopenharmony_ciconstructor of `T` accepts.
508f92157deSopenharmony_ci
509f92157deSopenharmony_ciFor example, the following code generates a test failure on the mock `my_mock`
510f92157deSopenharmony_ciof type `MockClass` if a method other than `DoSomething()` is called:
511f92157deSopenharmony_ci
512f92157deSopenharmony_ci```cpp
513f92157deSopenharmony_ciusing ::testing::StrictMock;
514f92157deSopenharmony_ci...
515f92157deSopenharmony_ciStrictMock<MockClass> my_mock("some", "args");
516f92157deSopenharmony_ciEXPECT_CALL(my_mock, DoSomething());
517f92157deSopenharmony_ci... code that uses my_mock ...
518f92157deSopenharmony_ci```
519f92157deSopenharmony_ci
520f92157deSopenharmony_ci`StrictMock<T>` only works for mock methods defined using the `MOCK_METHOD`
521f92157deSopenharmony_cimacro directly in the definition of class `T`. If a mock method is defined in a
522f92157deSopenharmony_cibase class of `T`, a failure might not be generated.
523f92157deSopenharmony_ci
524f92157deSopenharmony_ci`StrictMock<T>` might not work correctly if the destructor of `T` is not
525f92157deSopenharmony_civirtual.
526f92157deSopenharmony_ci
527f92157deSopenharmony_ci### Sequence {#Sequence}
528f92157deSopenharmony_ci
529f92157deSopenharmony_ci`::testing::Sequence`
530f92157deSopenharmony_ci
531f92157deSopenharmony_ciRepresents a chronological sequence of expectations. See the
532f92157deSopenharmony_ci[`InSequence`](#EXPECT_CALL.InSequence) clause of `EXPECT_CALL` for usage.
533f92157deSopenharmony_ci
534f92157deSopenharmony_ci### InSequence {#InSequence}
535f92157deSopenharmony_ci
536f92157deSopenharmony_ci`::testing::InSequence`
537f92157deSopenharmony_ci
538f92157deSopenharmony_ciAn object of this type causes all expectations encountered in its scope to be
539f92157deSopenharmony_ciput in an anonymous sequence.
540f92157deSopenharmony_ci
541f92157deSopenharmony_ciThis allows more convenient expression of multiple expectations in a single
542f92157deSopenharmony_cisequence:
543f92157deSopenharmony_ci
544f92157deSopenharmony_ci```cpp
545f92157deSopenharmony_ciusing ::testing::InSequence;
546f92157deSopenharmony_ci{
547f92157deSopenharmony_ci  InSequence seq;
548f92157deSopenharmony_ci
549f92157deSopenharmony_ci  // The following are expected to occur in the order declared.
550f92157deSopenharmony_ci  EXPECT_CALL(...);
551f92157deSopenharmony_ci  EXPECT_CALL(...);
552f92157deSopenharmony_ci  ...
553f92157deSopenharmony_ci  EXPECT_CALL(...);
554f92157deSopenharmony_ci}
555f92157deSopenharmony_ci```
556f92157deSopenharmony_ci
557f92157deSopenharmony_ciThe name of the `InSequence` object does not matter.
558f92157deSopenharmony_ci
559f92157deSopenharmony_ci### Expectation {#Expectation}
560f92157deSopenharmony_ci
561f92157deSopenharmony_ci`::testing::Expectation`
562f92157deSopenharmony_ci
563f92157deSopenharmony_ciRepresents a mock function call expectation as created by
564f92157deSopenharmony_ci[`EXPECT_CALL`](#EXPECT_CALL):
565f92157deSopenharmony_ci
566f92157deSopenharmony_ci```cpp
567f92157deSopenharmony_ciusing ::testing::Expectation;
568f92157deSopenharmony_ciExpectation my_expectation = EXPECT_CALL(...);
569f92157deSopenharmony_ci```
570f92157deSopenharmony_ci
571f92157deSopenharmony_ciUseful for specifying sequences of expectations; see the
572f92157deSopenharmony_ci[`After`](#EXPECT_CALL.After) clause of `EXPECT_CALL`.
573f92157deSopenharmony_ci
574f92157deSopenharmony_ci### ExpectationSet {#ExpectationSet}
575f92157deSopenharmony_ci
576f92157deSopenharmony_ci`::testing::ExpectationSet`
577f92157deSopenharmony_ci
578f92157deSopenharmony_ciRepresents a set of mock function call expectations.
579f92157deSopenharmony_ci
580f92157deSopenharmony_ciUse the `+=` operator to add [`Expectation`](#Expectation) objects to the set:
581f92157deSopenharmony_ci
582f92157deSopenharmony_ci```cpp
583f92157deSopenharmony_ciusing ::testing::ExpectationSet;
584f92157deSopenharmony_ciExpectationSet my_expectations;
585f92157deSopenharmony_cimy_expectations += EXPECT_CALL(...);
586f92157deSopenharmony_ci```
587f92157deSopenharmony_ci
588f92157deSopenharmony_ciUseful for specifying sequences of expectations; see the
589f92157deSopenharmony_ci[`After`](#EXPECT_CALL.After) clause of `EXPECT_CALL`.
590