128f6c2f2SEnji Cooper# Mocking Reference 228f6c2f2SEnji Cooper 328f6c2f2SEnji CooperThis page lists the facilities provided by GoogleTest for creating and working 4*5ca8c28cSEnji Cooperwith mock objects. To use them, add `#include <gmock/gmock.h>`. 528f6c2f2SEnji Cooper 628f6c2f2SEnji Cooper## Macros {#macros} 728f6c2f2SEnji Cooper 828f6c2f2SEnji CooperGoogleTest defines the following macros for working with mocks. 928f6c2f2SEnji Cooper 1028f6c2f2SEnji Cooper### MOCK_METHOD {#MOCK_METHOD} 1128f6c2f2SEnji Cooper 1228f6c2f2SEnji Cooper`MOCK_METHOD(`*`return_type`*`,`*`method_name`*`, (`*`args...`*`));` \ 1328f6c2f2SEnji Cooper`MOCK_METHOD(`*`return_type`*`,`*`method_name`*`, (`*`args...`*`), 1428f6c2f2SEnji Cooper(`*`specs...`*`));` 1528f6c2f2SEnji Cooper 1628f6c2f2SEnji CooperDefines a mock method *`method_name`* with arguments `(`*`args...`*`)` and 1728f6c2f2SEnji Cooperreturn type *`return_type`* within a mock class. 1828f6c2f2SEnji Cooper 1928f6c2f2SEnji CooperThe parameters of `MOCK_METHOD` mirror the method declaration. The optional 2028f6c2f2SEnji Cooperfourth parameter *`specs...`* is a comma-separated list of qualifiers. The 2128f6c2f2SEnji Cooperfollowing qualifiers are accepted: 2228f6c2f2SEnji Cooper 2328f6c2f2SEnji Cooper| Qualifier | Meaning | 2428f6c2f2SEnji Cooper| -------------------------- | -------------------------------------------- | 2528f6c2f2SEnji Cooper| `const` | Makes the mocked method a `const` method. Required if overriding a `const` method. | 2628f6c2f2SEnji Cooper| `override` | Marks the method with `override`. Recommended if overriding a `virtual` method. | 2728f6c2f2SEnji Cooper| `noexcept` | Marks the method with `noexcept`. Required if overriding a `noexcept` method. | 2828f6c2f2SEnji Cooper| `Calltype(`*`calltype`*`)` | Sets the call type for the method, for example `Calltype(STDMETHODCALLTYPE)`. Useful on Windows. | 2928f6c2f2SEnji Cooper| `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. | 3028f6c2f2SEnji Cooper 3128f6c2f2SEnji CooperNote that commas in arguments prevent `MOCK_METHOD` from parsing the arguments 3228f6c2f2SEnji Coopercorrectly if they are not appropriately surrounded by parentheses. See the 3328f6c2f2SEnji Cooperfollowing example: 3428f6c2f2SEnji Cooper 3528f6c2f2SEnji Cooper```cpp 3628f6c2f2SEnji Cooperclass MyMock { 3728f6c2f2SEnji Cooper public: 3828f6c2f2SEnji Cooper // The following 2 lines will not compile due to commas in the arguments: 3928f6c2f2SEnji Cooper MOCK_METHOD(std::pair<bool, int>, GetPair, ()); // Error! 4028f6c2f2SEnji Cooper MOCK_METHOD(bool, CheckMap, (std::map<int, double>, bool)); // Error! 4128f6c2f2SEnji Cooper 4228f6c2f2SEnji Cooper // One solution - wrap arguments that contain commas in parentheses: 4328f6c2f2SEnji Cooper MOCK_METHOD((std::pair<bool, int>), GetPair, ()); 4428f6c2f2SEnji Cooper MOCK_METHOD(bool, CheckMap, ((std::map<int, double>), bool)); 4528f6c2f2SEnji Cooper 4628f6c2f2SEnji Cooper // Another solution - use type aliases: 4728f6c2f2SEnji Cooper using BoolAndInt = std::pair<bool, int>; 4828f6c2f2SEnji Cooper MOCK_METHOD(BoolAndInt, GetPair, ()); 4928f6c2f2SEnji Cooper using MapIntDouble = std::map<int, double>; 5028f6c2f2SEnji Cooper MOCK_METHOD(bool, CheckMap, (MapIntDouble, bool)); 5128f6c2f2SEnji Cooper}; 5228f6c2f2SEnji Cooper``` 5328f6c2f2SEnji Cooper 5428f6c2f2SEnji Cooper`MOCK_METHOD` must be used in the `public:` section of a mock class definition, 5528f6c2f2SEnji Cooperregardless of whether the method being mocked is `public`, `protected`, or 5628f6c2f2SEnji Cooper`private` in the base class. 5728f6c2f2SEnji Cooper 5828f6c2f2SEnji Cooper### EXPECT_CALL {#EXPECT_CALL} 5928f6c2f2SEnji Cooper 6028f6c2f2SEnji Cooper`EXPECT_CALL(`*`mock_object`*`,`*`method_name`*`(`*`matchers...`*`))` 6128f6c2f2SEnji Cooper 6228f6c2f2SEnji CooperCreates an [expectation](../gmock_for_dummies.md#setting-expectations) that the 6328f6c2f2SEnji Coopermethod *`method_name`* of the object *`mock_object`* is called with arguments 6428f6c2f2SEnji Cooperthat match the given matchers *`matchers...`*. `EXPECT_CALL` must precede any 6528f6c2f2SEnji Coopercode that exercises the mock object. 6628f6c2f2SEnji Cooper 6728f6c2f2SEnji CooperThe parameter *`matchers...`* is a comma-separated list of 6828f6c2f2SEnji Cooper[matchers](../gmock_for_dummies.md#matchers-what-arguments-do-we-expect) that 6928f6c2f2SEnji Coopercorrespond to each argument of the method *`method_name`*. The expectation will 7028f6c2f2SEnji Cooperapply only to calls of *`method_name`* whose arguments match all of the 7128f6c2f2SEnji Coopermatchers. If `(`*`matchers...`*`)` is omitted, the expectation behaves as if 7228f6c2f2SEnji Coopereach argument's matcher were a [wildcard matcher (`_`)](matchers.md#wildcard). 7328f6c2f2SEnji CooperSee the [Matchers Reference](matchers.md) for a list of all built-in matchers. 7428f6c2f2SEnji Cooper 7528f6c2f2SEnji CooperThe following chainable clauses can be used to modify the expectation, and they 7628f6c2f2SEnji Coopermust be used in the following order: 7728f6c2f2SEnji Cooper 7828f6c2f2SEnji Cooper```cpp 7928f6c2f2SEnji CooperEXPECT_CALL(mock_object, method_name(matchers...)) 8028f6c2f2SEnji Cooper .With(multi_argument_matcher) // Can be used at most once 8128f6c2f2SEnji Cooper .Times(cardinality) // Can be used at most once 8228f6c2f2SEnji Cooper .InSequence(sequences...) // Can be used any number of times 8328f6c2f2SEnji Cooper .After(expectations...) // Can be used any number of times 8428f6c2f2SEnji Cooper .WillOnce(action) // Can be used any number of times 8528f6c2f2SEnji Cooper .WillRepeatedly(action) // Can be used at most once 8628f6c2f2SEnji Cooper .RetiresOnSaturation(); // Can be used at most once 8728f6c2f2SEnji Cooper``` 8828f6c2f2SEnji Cooper 8928f6c2f2SEnji CooperSee details for each modifier clause below. 9028f6c2f2SEnji Cooper 9128f6c2f2SEnji Cooper#### With {#EXPECT_CALL.With} 9228f6c2f2SEnji Cooper 9328f6c2f2SEnji Cooper`.With(`*`multi_argument_matcher`*`)` 9428f6c2f2SEnji Cooper 9528f6c2f2SEnji CooperRestricts the expectation to apply only to mock function calls whose arguments 9628f6c2f2SEnji Cooperas a whole match the multi-argument matcher *`multi_argument_matcher`*. 9728f6c2f2SEnji Cooper 9828f6c2f2SEnji CooperGoogleTest passes all of the arguments as one tuple into the matcher. The 9928f6c2f2SEnji Cooperparameter *`multi_argument_matcher`* must thus be a matcher of type 10028f6c2f2SEnji Cooper`Matcher<std::tuple<A1, ..., An>>`, where `A1, ..., An` are the types of the 10128f6c2f2SEnji Cooperfunction arguments. 10228f6c2f2SEnji Cooper 10328f6c2f2SEnji CooperFor example, the following code sets the expectation that 10428f6c2f2SEnji Cooper`my_mock.SetPosition()` is called with any two arguments, the first argument 10528f6c2f2SEnji Cooperbeing less than the second: 10628f6c2f2SEnji Cooper 10728f6c2f2SEnji Cooper```cpp 10828f6c2f2SEnji Cooperusing ::testing::_; 10928f6c2f2SEnji Cooperusing ::testing::Lt; 11028f6c2f2SEnji Cooper... 11128f6c2f2SEnji CooperEXPECT_CALL(my_mock, SetPosition(_, _)) 11228f6c2f2SEnji Cooper .With(Lt()); 11328f6c2f2SEnji Cooper``` 11428f6c2f2SEnji Cooper 11528f6c2f2SEnji CooperGoogleTest provides some built-in matchers for 2-tuples, including the `Lt()` 11628f6c2f2SEnji Coopermatcher above. See [Multi-argument Matchers](matchers.md#MultiArgMatchers). 11728f6c2f2SEnji Cooper 11828f6c2f2SEnji CooperThe `With` clause can be used at most once on an expectation and must be the 11928f6c2f2SEnji Cooperfirst clause. 12028f6c2f2SEnji Cooper 12128f6c2f2SEnji Cooper#### Times {#EXPECT_CALL.Times} 12228f6c2f2SEnji Cooper 12328f6c2f2SEnji Cooper`.Times(`*`cardinality`*`)` 12428f6c2f2SEnji Cooper 12528f6c2f2SEnji CooperSpecifies how many times the mock function call is expected. 12628f6c2f2SEnji Cooper 12728f6c2f2SEnji CooperThe parameter *`cardinality`* represents the number of expected calls and can be 12828f6c2f2SEnji Cooperone of the following, all defined in the `::testing` namespace: 12928f6c2f2SEnji Cooper 13028f6c2f2SEnji Cooper| Cardinality | Meaning | 13128f6c2f2SEnji Cooper| ------------------- | --------------------------------------------------- | 13228f6c2f2SEnji Cooper| `AnyNumber()` | The function can be called any number of times. | 13328f6c2f2SEnji Cooper| `AtLeast(n)` | The function call is expected at least *n* times. | 13428f6c2f2SEnji Cooper| `AtMost(n)` | The function call is expected at most *n* times. | 13528f6c2f2SEnji Cooper| `Between(m, n)` | The function call is expected between *m* and *n* times, inclusive. | 13628f6c2f2SEnji Cooper| `Exactly(n)` or `n` | The function call is expected exactly *n* times. If *n* is 0, the call should never happen. | 13728f6c2f2SEnji Cooper 13828f6c2f2SEnji CooperIf the `Times` clause is omitted, GoogleTest infers the cardinality as follows: 13928f6c2f2SEnji Cooper 14028f6c2f2SEnji Cooper* If neither [`WillOnce`](#EXPECT_CALL.WillOnce) nor 14128f6c2f2SEnji Cooper [`WillRepeatedly`](#EXPECT_CALL.WillRepeatedly) are specified, the inferred 14228f6c2f2SEnji Cooper cardinality is `Times(1)`. 14328f6c2f2SEnji Cooper* If there are *n* `WillOnce` clauses and no `WillRepeatedly` clause, where 14428f6c2f2SEnji Cooper *n* >= 1, the inferred cardinality is `Times(n)`. 14528f6c2f2SEnji Cooper* If there are *n* `WillOnce` clauses and one `WillRepeatedly` clause, where 14628f6c2f2SEnji Cooper *n* >= 0, the inferred cardinality is `Times(AtLeast(n))`. 14728f6c2f2SEnji Cooper 14828f6c2f2SEnji CooperThe `Times` clause can be used at most once on an expectation. 14928f6c2f2SEnji Cooper 15028f6c2f2SEnji Cooper#### InSequence {#EXPECT_CALL.InSequence} 15128f6c2f2SEnji Cooper 15228f6c2f2SEnji Cooper`.InSequence(`*`sequences...`*`)` 15328f6c2f2SEnji Cooper 15428f6c2f2SEnji CooperSpecifies that the mock function call is expected in a certain sequence. 15528f6c2f2SEnji Cooper 15628f6c2f2SEnji CooperThe parameter *`sequences...`* is any number of [`Sequence`](#Sequence) objects. 15728f6c2f2SEnji CooperExpected calls assigned to the same sequence are expected to occur in the order 15828f6c2f2SEnji Cooperthe expectations are declared. 15928f6c2f2SEnji Cooper 16028f6c2f2SEnji CooperFor example, the following code sets the expectation that the `Reset()` method 16128f6c2f2SEnji Cooperof `my_mock` is called before both `GetSize()` and `Describe()`, and `GetSize()` 16228f6c2f2SEnji Cooperand `Describe()` can occur in any order relative to each other: 16328f6c2f2SEnji Cooper 16428f6c2f2SEnji Cooper```cpp 16528f6c2f2SEnji Cooperusing ::testing::Sequence; 16628f6c2f2SEnji CooperSequence s1, s2; 16728f6c2f2SEnji Cooper... 16828f6c2f2SEnji CooperEXPECT_CALL(my_mock, Reset()) 16928f6c2f2SEnji Cooper .InSequence(s1, s2); 17028f6c2f2SEnji CooperEXPECT_CALL(my_mock, GetSize()) 17128f6c2f2SEnji Cooper .InSequence(s1); 17228f6c2f2SEnji CooperEXPECT_CALL(my_mock, Describe()) 17328f6c2f2SEnji Cooper .InSequence(s2); 17428f6c2f2SEnji Cooper``` 17528f6c2f2SEnji Cooper 17628f6c2f2SEnji CooperThe `InSequence` clause can be used any number of times on an expectation. 17728f6c2f2SEnji Cooper 17828f6c2f2SEnji CooperSee also the [`InSequence` class](#InSequence). 17928f6c2f2SEnji Cooper 18028f6c2f2SEnji Cooper#### After {#EXPECT_CALL.After} 18128f6c2f2SEnji Cooper 18228f6c2f2SEnji Cooper`.After(`*`expectations...`*`)` 18328f6c2f2SEnji Cooper 18428f6c2f2SEnji CooperSpecifies that the mock function call is expected to occur after one or more 18528f6c2f2SEnji Cooperother calls. 18628f6c2f2SEnji Cooper 18728f6c2f2SEnji CooperThe parameter *`expectations...`* can be up to five 18828f6c2f2SEnji Cooper[`Expectation`](#Expectation) or [`ExpectationSet`](#ExpectationSet) objects. 18928f6c2f2SEnji CooperThe mock function call is expected to occur after all of the given expectations. 19028f6c2f2SEnji Cooper 19128f6c2f2SEnji CooperFor example, the following code sets the expectation that the `Describe()` 19228f6c2f2SEnji Coopermethod of `my_mock` is called only after both `InitX()` and `InitY()` have been 19328f6c2f2SEnji Coopercalled. 19428f6c2f2SEnji Cooper 19528f6c2f2SEnji Cooper```cpp 19628f6c2f2SEnji Cooperusing ::testing::Expectation; 19728f6c2f2SEnji Cooper... 19828f6c2f2SEnji CooperExpectation init_x = EXPECT_CALL(my_mock, InitX()); 19928f6c2f2SEnji CooperExpectation init_y = EXPECT_CALL(my_mock, InitY()); 20028f6c2f2SEnji CooperEXPECT_CALL(my_mock, Describe()) 20128f6c2f2SEnji Cooper .After(init_x, init_y); 20228f6c2f2SEnji Cooper``` 20328f6c2f2SEnji Cooper 20428f6c2f2SEnji CooperThe `ExpectationSet` object is helpful when the number of prerequisites for an 20528f6c2f2SEnji Cooperexpectation is large or variable, for example: 20628f6c2f2SEnji Cooper 20728f6c2f2SEnji Cooper```cpp 20828f6c2f2SEnji Cooperusing ::testing::ExpectationSet; 20928f6c2f2SEnji Cooper... 21028f6c2f2SEnji CooperExpectationSet all_inits; 21128f6c2f2SEnji Cooper// Collect all expectations of InitElement() calls 21228f6c2f2SEnji Cooperfor (int i = 0; i < element_count; i++) { 21328f6c2f2SEnji Cooper all_inits += EXPECT_CALL(my_mock, InitElement(i)); 21428f6c2f2SEnji Cooper} 21528f6c2f2SEnji CooperEXPECT_CALL(my_mock, Describe()) 21628f6c2f2SEnji Cooper .After(all_inits); // Expect Describe() call after all InitElement() calls 21728f6c2f2SEnji Cooper``` 21828f6c2f2SEnji Cooper 21928f6c2f2SEnji CooperThe `After` clause can be used any number of times on an expectation. 22028f6c2f2SEnji Cooper 22128f6c2f2SEnji Cooper#### WillOnce {#EXPECT_CALL.WillOnce} 22228f6c2f2SEnji Cooper 22328f6c2f2SEnji Cooper`.WillOnce(`*`action`*`)` 22428f6c2f2SEnji Cooper 22528f6c2f2SEnji CooperSpecifies the mock function's actual behavior when invoked, for a single 22628f6c2f2SEnji Coopermatching function call. 22728f6c2f2SEnji Cooper 22828f6c2f2SEnji CooperThe parameter *`action`* represents the 22928f6c2f2SEnji Cooper[action](../gmock_for_dummies.md#actions-what-should-it-do) that the function 23028f6c2f2SEnji Coopercall will perform. See the [Actions Reference](actions.md) for a list of 23128f6c2f2SEnji Cooperbuilt-in actions. 23228f6c2f2SEnji Cooper 23328f6c2f2SEnji CooperThe use of `WillOnce` implicitly sets a cardinality on the expectation when 23428f6c2f2SEnji Cooper`Times` is not specified. See [`Times`](#EXPECT_CALL.Times). 23528f6c2f2SEnji Cooper 23628f6c2f2SEnji CooperEach matching function call will perform the next action in the order declared. 23728f6c2f2SEnji CooperFor example, the following code specifies that `my_mock.GetNumber()` is expected 23828f6c2f2SEnji Cooperto be called exactly 3 times and will return `1`, `2`, and `3` respectively on 23928f6c2f2SEnji Cooperthe first, second, and third calls: 24028f6c2f2SEnji Cooper 24128f6c2f2SEnji Cooper```cpp 24228f6c2f2SEnji Cooperusing ::testing::Return; 24328f6c2f2SEnji Cooper... 24428f6c2f2SEnji CooperEXPECT_CALL(my_mock, GetNumber()) 24528f6c2f2SEnji Cooper .WillOnce(Return(1)) 24628f6c2f2SEnji Cooper .WillOnce(Return(2)) 24728f6c2f2SEnji Cooper .WillOnce(Return(3)); 24828f6c2f2SEnji Cooper``` 24928f6c2f2SEnji Cooper 25028f6c2f2SEnji CooperThe `WillOnce` clause can be used any number of times on an expectation. Unlike 25128f6c2f2SEnji Cooper`WillRepeatedly`, the action fed to each `WillOnce` call will be called at most 25228f6c2f2SEnji Cooperonce, so may be a move-only type and/or have an `&&`-qualified call operator. 25328f6c2f2SEnji Cooper 25428f6c2f2SEnji Cooper#### WillRepeatedly {#EXPECT_CALL.WillRepeatedly} 25528f6c2f2SEnji Cooper 25628f6c2f2SEnji Cooper`.WillRepeatedly(`*`action`*`)` 25728f6c2f2SEnji Cooper 25828f6c2f2SEnji CooperSpecifies the mock function's actual behavior when invoked, for all subsequent 25928f6c2f2SEnji Coopermatching function calls. Takes effect after the actions specified in the 26028f6c2f2SEnji Cooper[`WillOnce`](#EXPECT_CALL.WillOnce) clauses, if any, have been performed. 26128f6c2f2SEnji Cooper 26228f6c2f2SEnji CooperThe parameter *`action`* represents the 26328f6c2f2SEnji Cooper[action](../gmock_for_dummies.md#actions-what-should-it-do) that the function 26428f6c2f2SEnji Coopercall will perform. See the [Actions Reference](actions.md) for a list of 26528f6c2f2SEnji Cooperbuilt-in actions. 26628f6c2f2SEnji Cooper 26728f6c2f2SEnji CooperThe use of `WillRepeatedly` implicitly sets a cardinality on the expectation 26828f6c2f2SEnji Cooperwhen `Times` is not specified. See [`Times`](#EXPECT_CALL.Times). 26928f6c2f2SEnji Cooper 27028f6c2f2SEnji CooperIf any `WillOnce` clauses have been specified, matching function calls will 27128f6c2f2SEnji Cooperperform those actions before the action specified by `WillRepeatedly`. See the 27228f6c2f2SEnji Cooperfollowing example: 27328f6c2f2SEnji Cooper 27428f6c2f2SEnji Cooper```cpp 27528f6c2f2SEnji Cooperusing ::testing::Return; 27628f6c2f2SEnji Cooper... 27728f6c2f2SEnji CooperEXPECT_CALL(my_mock, GetName()) 27828f6c2f2SEnji Cooper .WillRepeatedly(Return("John Doe")); // Return "John Doe" on all calls 27928f6c2f2SEnji Cooper 28028f6c2f2SEnji CooperEXPECT_CALL(my_mock, GetNumber()) 28128f6c2f2SEnji Cooper .WillOnce(Return(42)) // Return 42 on the first call 28228f6c2f2SEnji Cooper .WillRepeatedly(Return(7)); // Return 7 on all subsequent calls 28328f6c2f2SEnji Cooper``` 28428f6c2f2SEnji Cooper 28528f6c2f2SEnji CooperThe `WillRepeatedly` clause can be used at most once on an expectation. 28628f6c2f2SEnji Cooper 28728f6c2f2SEnji Cooper#### RetiresOnSaturation {#EXPECT_CALL.RetiresOnSaturation} 28828f6c2f2SEnji Cooper 28928f6c2f2SEnji Cooper`.RetiresOnSaturation()` 29028f6c2f2SEnji Cooper 29128f6c2f2SEnji CooperIndicates that the expectation will no longer be active after the expected 29228f6c2f2SEnji Coopernumber of matching function calls has been reached. 29328f6c2f2SEnji Cooper 29428f6c2f2SEnji CooperThe `RetiresOnSaturation` clause is only meaningful for expectations with an 29528f6c2f2SEnji Cooperupper-bounded cardinality. The expectation will *retire* (no longer match any 29628f6c2f2SEnji Cooperfunction calls) after it has been *saturated* (the upper bound has been 29728f6c2f2SEnji Cooperreached). See the following example: 29828f6c2f2SEnji Cooper 29928f6c2f2SEnji Cooper```cpp 30028f6c2f2SEnji Cooperusing ::testing::_; 30128f6c2f2SEnji Cooperusing ::testing::AnyNumber; 30228f6c2f2SEnji Cooper... 30328f6c2f2SEnji CooperEXPECT_CALL(my_mock, SetNumber(_)) // Expectation 1 30428f6c2f2SEnji Cooper .Times(AnyNumber()); 30528f6c2f2SEnji CooperEXPECT_CALL(my_mock, SetNumber(7)) // Expectation 2 30628f6c2f2SEnji Cooper .Times(2) 30728f6c2f2SEnji Cooper .RetiresOnSaturation(); 30828f6c2f2SEnji Cooper``` 30928f6c2f2SEnji Cooper 31028f6c2f2SEnji CooperIn the above example, the first two calls to `my_mock.SetNumber(7)` match 31128f6c2f2SEnji Cooperexpectation 2, which then becomes inactive and no longer matches any calls. A 31228f6c2f2SEnji Cooperthird call to `my_mock.SetNumber(7)` would then match expectation 1. Without 31328f6c2f2SEnji Cooper`RetiresOnSaturation()` on expectation 2, a third call to `my_mock.SetNumber(7)` 31428f6c2f2SEnji Cooperwould match expectation 2 again, producing a failure since the limit of 2 calls 31528f6c2f2SEnji Cooperwas exceeded. 31628f6c2f2SEnji Cooper 31728f6c2f2SEnji CooperThe `RetiresOnSaturation` clause can be used at most once on an expectation and 31828f6c2f2SEnji Coopermust be the last clause. 31928f6c2f2SEnji Cooper 32028f6c2f2SEnji Cooper### ON_CALL {#ON_CALL} 32128f6c2f2SEnji Cooper 32228f6c2f2SEnji Cooper`ON_CALL(`*`mock_object`*`,`*`method_name`*`(`*`matchers...`*`))` 32328f6c2f2SEnji Cooper 32428f6c2f2SEnji CooperDefines what happens when the method *`method_name`* of the object 32528f6c2f2SEnji Cooper*`mock_object`* is called with arguments that match the given matchers 32628f6c2f2SEnji Cooper*`matchers...`*. Requires a modifier clause to specify the method's behavior. 32728f6c2f2SEnji Cooper*Does not* set any expectations that the method will be called. 32828f6c2f2SEnji Cooper 32928f6c2f2SEnji CooperThe parameter *`matchers...`* is a comma-separated list of 33028f6c2f2SEnji Cooper[matchers](../gmock_for_dummies.md#matchers-what-arguments-do-we-expect) that 33128f6c2f2SEnji Coopercorrespond to each argument of the method *`method_name`*. The `ON_CALL` 33228f6c2f2SEnji Cooperspecification will apply only to calls of *`method_name`* whose arguments match 33328f6c2f2SEnji Cooperall of the matchers. If `(`*`matchers...`*`)` is omitted, the behavior is as if 33428f6c2f2SEnji Coopereach argument's matcher were a [wildcard matcher (`_`)](matchers.md#wildcard). 33528f6c2f2SEnji CooperSee the [Matchers Reference](matchers.md) for a list of all built-in matchers. 33628f6c2f2SEnji Cooper 33728f6c2f2SEnji CooperThe following chainable clauses can be used to set the method's behavior, and 33828f6c2f2SEnji Cooperthey must be used in the following order: 33928f6c2f2SEnji Cooper 34028f6c2f2SEnji Cooper```cpp 34128f6c2f2SEnji CooperON_CALL(mock_object, method_name(matchers...)) 34228f6c2f2SEnji Cooper .With(multi_argument_matcher) // Can be used at most once 34328f6c2f2SEnji Cooper .WillByDefault(action); // Required 34428f6c2f2SEnji Cooper``` 34528f6c2f2SEnji Cooper 34628f6c2f2SEnji CooperSee details for each modifier clause below. 34728f6c2f2SEnji Cooper 34828f6c2f2SEnji Cooper#### With {#ON_CALL.With} 34928f6c2f2SEnji Cooper 35028f6c2f2SEnji Cooper`.With(`*`multi_argument_matcher`*`)` 35128f6c2f2SEnji Cooper 35228f6c2f2SEnji CooperRestricts the specification to only mock function calls whose arguments as a 35328f6c2f2SEnji Cooperwhole match the multi-argument matcher *`multi_argument_matcher`*. 35428f6c2f2SEnji Cooper 35528f6c2f2SEnji CooperGoogleTest passes all of the arguments as one tuple into the matcher. The 35628f6c2f2SEnji Cooperparameter *`multi_argument_matcher`* must thus be a matcher of type 35728f6c2f2SEnji Cooper`Matcher<std::tuple<A1, ..., An>>`, where `A1, ..., An` are the types of the 35828f6c2f2SEnji Cooperfunction arguments. 35928f6c2f2SEnji Cooper 36028f6c2f2SEnji CooperFor example, the following code sets the default behavior when 36128f6c2f2SEnji Cooper`my_mock.SetPosition()` is called with any two arguments, the first argument 36228f6c2f2SEnji Cooperbeing less than the second: 36328f6c2f2SEnji Cooper 36428f6c2f2SEnji Cooper```cpp 36528f6c2f2SEnji Cooperusing ::testing::_; 36628f6c2f2SEnji Cooperusing ::testing::Lt; 36728f6c2f2SEnji Cooperusing ::testing::Return; 36828f6c2f2SEnji Cooper... 36928f6c2f2SEnji CooperON_CALL(my_mock, SetPosition(_, _)) 37028f6c2f2SEnji Cooper .With(Lt()) 37128f6c2f2SEnji Cooper .WillByDefault(Return(true)); 37228f6c2f2SEnji Cooper``` 37328f6c2f2SEnji Cooper 37428f6c2f2SEnji CooperGoogleTest provides some built-in matchers for 2-tuples, including the `Lt()` 37528f6c2f2SEnji Coopermatcher above. See [Multi-argument Matchers](matchers.md#MultiArgMatchers). 37628f6c2f2SEnji Cooper 37728f6c2f2SEnji CooperThe `With` clause can be used at most once with each `ON_CALL` statement. 37828f6c2f2SEnji Cooper 37928f6c2f2SEnji Cooper#### WillByDefault {#ON_CALL.WillByDefault} 38028f6c2f2SEnji Cooper 38128f6c2f2SEnji Cooper`.WillByDefault(`*`action`*`)` 38228f6c2f2SEnji Cooper 38328f6c2f2SEnji CooperSpecifies the default behavior of a matching mock function call. 38428f6c2f2SEnji Cooper 38528f6c2f2SEnji CooperThe parameter *`action`* represents the 38628f6c2f2SEnji Cooper[action](../gmock_for_dummies.md#actions-what-should-it-do) that the function 38728f6c2f2SEnji Coopercall will perform. See the [Actions Reference](actions.md) for a list of 38828f6c2f2SEnji Cooperbuilt-in actions. 38928f6c2f2SEnji Cooper 39028f6c2f2SEnji CooperFor example, the following code specifies that by default, a call to 39128f6c2f2SEnji Cooper`my_mock.Greet()` will return `"hello"`: 39228f6c2f2SEnji Cooper 39328f6c2f2SEnji Cooper```cpp 39428f6c2f2SEnji Cooperusing ::testing::Return; 39528f6c2f2SEnji Cooper... 39628f6c2f2SEnji CooperON_CALL(my_mock, Greet()) 39728f6c2f2SEnji Cooper .WillByDefault(Return("hello")); 39828f6c2f2SEnji Cooper``` 39928f6c2f2SEnji Cooper 40028f6c2f2SEnji CooperThe action specified by `WillByDefault` is superseded by the actions specified 40128f6c2f2SEnji Cooperon a matching `EXPECT_CALL` statement, if any. See the 40228f6c2f2SEnji Cooper[`WillOnce`](#EXPECT_CALL.WillOnce) and 40328f6c2f2SEnji Cooper[`WillRepeatedly`](#EXPECT_CALL.WillRepeatedly) clauses of `EXPECT_CALL`. 40428f6c2f2SEnji Cooper 40528f6c2f2SEnji CooperThe `WillByDefault` clause must be used exactly once with each `ON_CALL` 40628f6c2f2SEnji Cooperstatement. 40728f6c2f2SEnji Cooper 40828f6c2f2SEnji Cooper## Classes {#classes} 40928f6c2f2SEnji Cooper 41028f6c2f2SEnji CooperGoogleTest defines the following classes for working with mocks. 41128f6c2f2SEnji Cooper 41228f6c2f2SEnji Cooper### DefaultValue {#DefaultValue} 41328f6c2f2SEnji Cooper 41428f6c2f2SEnji Cooper`::testing::DefaultValue<T>` 41528f6c2f2SEnji Cooper 41628f6c2f2SEnji CooperAllows a user to specify the default value for a type `T` that is both copyable 41728f6c2f2SEnji Cooperand publicly destructible (i.e. anything that can be used as a function return 41828f6c2f2SEnji Coopertype). For mock functions with a return type of `T`, this default value is 41928f6c2f2SEnji Cooperreturned from function calls that do not specify an action. 42028f6c2f2SEnji Cooper 42128f6c2f2SEnji CooperProvides the static methods `Set()`, `SetFactory()`, and `Clear()` to manage the 42228f6c2f2SEnji Cooperdefault value: 42328f6c2f2SEnji Cooper 42428f6c2f2SEnji Cooper```cpp 42528f6c2f2SEnji Cooper// Sets the default value to be returned. T must be copy constructible. 42628f6c2f2SEnji CooperDefaultValue<T>::Set(value); 42728f6c2f2SEnji Cooper 42828f6c2f2SEnji Cooper// Sets a factory. Will be invoked on demand. T must be move constructible. 42928f6c2f2SEnji CooperT MakeT(); 43028f6c2f2SEnji CooperDefaultValue<T>::SetFactory(&MakeT); 43128f6c2f2SEnji Cooper 43228f6c2f2SEnji Cooper// Unsets the default value. 43328f6c2f2SEnji CooperDefaultValue<T>::Clear(); 43428f6c2f2SEnji Cooper``` 43528f6c2f2SEnji Cooper 43628f6c2f2SEnji Cooper### NiceMock {#NiceMock} 43728f6c2f2SEnji Cooper 43828f6c2f2SEnji Cooper`::testing::NiceMock<T>` 43928f6c2f2SEnji Cooper 44028f6c2f2SEnji CooperRepresents a mock object that suppresses warnings on 44128f6c2f2SEnji Cooper[uninteresting calls](../gmock_cook_book.md#uninteresting-vs-unexpected). The 44228f6c2f2SEnji Coopertemplate parameter `T` is any mock class, except for another `NiceMock`, 44328f6c2f2SEnji Cooper`NaggyMock`, or `StrictMock`. 44428f6c2f2SEnji Cooper 44528f6c2f2SEnji CooperUsage of `NiceMock<T>` is analogous to usage of `T`. `NiceMock<T>` is a subclass 44628f6c2f2SEnji Cooperof `T`, so it can be used wherever an object of type `T` is accepted. In 44728f6c2f2SEnji Cooperaddition, `NiceMock<T>` can be constructed with any arguments that a constructor 44828f6c2f2SEnji Cooperof `T` accepts. 44928f6c2f2SEnji Cooper 45028f6c2f2SEnji CooperFor example, the following code suppresses warnings on the mock `my_mock` of 45128f6c2f2SEnji Coopertype `MockClass` if a method other than `DoSomething()` is called: 45228f6c2f2SEnji Cooper 45328f6c2f2SEnji Cooper```cpp 45428f6c2f2SEnji Cooperusing ::testing::NiceMock; 45528f6c2f2SEnji Cooper... 45628f6c2f2SEnji CooperNiceMock<MockClass> my_mock("some", "args"); 45728f6c2f2SEnji CooperEXPECT_CALL(my_mock, DoSomething()); 45828f6c2f2SEnji Cooper... code that uses my_mock ... 45928f6c2f2SEnji Cooper``` 46028f6c2f2SEnji Cooper 46128f6c2f2SEnji Cooper`NiceMock<T>` only works for mock methods defined using the `MOCK_METHOD` macro 46228f6c2f2SEnji Cooperdirectly in the definition of class `T`. If a mock method is defined in a base 46328f6c2f2SEnji Cooperclass of `T`, a warning might still be generated. 46428f6c2f2SEnji Cooper 46528f6c2f2SEnji Cooper`NiceMock<T>` might not work correctly if the destructor of `T` is not virtual. 46628f6c2f2SEnji Cooper 46728f6c2f2SEnji Cooper### NaggyMock {#NaggyMock} 46828f6c2f2SEnji Cooper 46928f6c2f2SEnji Cooper`::testing::NaggyMock<T>` 47028f6c2f2SEnji Cooper 47128f6c2f2SEnji CooperRepresents a mock object that generates warnings on 47228f6c2f2SEnji Cooper[uninteresting calls](../gmock_cook_book.md#uninteresting-vs-unexpected). The 47328f6c2f2SEnji Coopertemplate parameter `T` is any mock class, except for another `NiceMock`, 47428f6c2f2SEnji Cooper`NaggyMock`, or `StrictMock`. 47528f6c2f2SEnji Cooper 47628f6c2f2SEnji CooperUsage of `NaggyMock<T>` is analogous to usage of `T`. `NaggyMock<T>` is a 47728f6c2f2SEnji Coopersubclass of `T`, so it can be used wherever an object of type `T` is accepted. 47828f6c2f2SEnji CooperIn addition, `NaggyMock<T>` can be constructed with any arguments that a 47928f6c2f2SEnji Cooperconstructor of `T` accepts. 48028f6c2f2SEnji Cooper 48128f6c2f2SEnji CooperFor example, the following code generates warnings on the mock `my_mock` of type 48228f6c2f2SEnji Cooper`MockClass` if a method other than `DoSomething()` is called: 48328f6c2f2SEnji Cooper 48428f6c2f2SEnji Cooper```cpp 48528f6c2f2SEnji Cooperusing ::testing::NaggyMock; 48628f6c2f2SEnji Cooper... 48728f6c2f2SEnji CooperNaggyMock<MockClass> my_mock("some", "args"); 48828f6c2f2SEnji CooperEXPECT_CALL(my_mock, DoSomething()); 48928f6c2f2SEnji Cooper... code that uses my_mock ... 49028f6c2f2SEnji Cooper``` 49128f6c2f2SEnji Cooper 49228f6c2f2SEnji CooperMock objects of type `T` by default behave the same way as `NaggyMock<T>`. 49328f6c2f2SEnji Cooper 49428f6c2f2SEnji Cooper### StrictMock {#StrictMock} 49528f6c2f2SEnji Cooper 49628f6c2f2SEnji Cooper`::testing::StrictMock<T>` 49728f6c2f2SEnji Cooper 49828f6c2f2SEnji CooperRepresents a mock object that generates test failures on 49928f6c2f2SEnji Cooper[uninteresting calls](../gmock_cook_book.md#uninteresting-vs-unexpected). The 50028f6c2f2SEnji Coopertemplate parameter `T` is any mock class, except for another `NiceMock`, 50128f6c2f2SEnji Cooper`NaggyMock`, or `StrictMock`. 50228f6c2f2SEnji Cooper 50328f6c2f2SEnji CooperUsage of `StrictMock<T>` is analogous to usage of `T`. `StrictMock<T>` is a 50428f6c2f2SEnji Coopersubclass of `T`, so it can be used wherever an object of type `T` is accepted. 50528f6c2f2SEnji CooperIn addition, `StrictMock<T>` can be constructed with any arguments that a 50628f6c2f2SEnji Cooperconstructor of `T` accepts. 50728f6c2f2SEnji Cooper 50828f6c2f2SEnji CooperFor example, the following code generates a test failure on the mock `my_mock` 50928f6c2f2SEnji Cooperof type `MockClass` if a method other than `DoSomething()` is called: 51028f6c2f2SEnji Cooper 51128f6c2f2SEnji Cooper```cpp 51228f6c2f2SEnji Cooperusing ::testing::StrictMock; 51328f6c2f2SEnji Cooper... 51428f6c2f2SEnji CooperStrictMock<MockClass> my_mock("some", "args"); 51528f6c2f2SEnji CooperEXPECT_CALL(my_mock, DoSomething()); 51628f6c2f2SEnji Cooper... code that uses my_mock ... 51728f6c2f2SEnji Cooper``` 51828f6c2f2SEnji Cooper 51928f6c2f2SEnji Cooper`StrictMock<T>` only works for mock methods defined using the `MOCK_METHOD` 52028f6c2f2SEnji Coopermacro directly in the definition of class `T`. If a mock method is defined in a 52128f6c2f2SEnji Cooperbase class of `T`, a failure might not be generated. 52228f6c2f2SEnji Cooper 52328f6c2f2SEnji Cooper`StrictMock<T>` might not work correctly if the destructor of `T` is not 52428f6c2f2SEnji Coopervirtual. 52528f6c2f2SEnji Cooper 52628f6c2f2SEnji Cooper### Sequence {#Sequence} 52728f6c2f2SEnji Cooper 52828f6c2f2SEnji Cooper`::testing::Sequence` 52928f6c2f2SEnji Cooper 53028f6c2f2SEnji CooperRepresents a chronological sequence of expectations. See the 53128f6c2f2SEnji Cooper[`InSequence`](#EXPECT_CALL.InSequence) clause of `EXPECT_CALL` for usage. 53228f6c2f2SEnji Cooper 53328f6c2f2SEnji Cooper### InSequence {#InSequence} 53428f6c2f2SEnji Cooper 53528f6c2f2SEnji Cooper`::testing::InSequence` 53628f6c2f2SEnji Cooper 53728f6c2f2SEnji CooperAn object of this type causes all expectations encountered in its scope to be 53828f6c2f2SEnji Cooperput in an anonymous sequence. 53928f6c2f2SEnji Cooper 54028f6c2f2SEnji CooperThis allows more convenient expression of multiple expectations in a single 54128f6c2f2SEnji Coopersequence: 54228f6c2f2SEnji Cooper 54328f6c2f2SEnji Cooper```cpp 54428f6c2f2SEnji Cooperusing ::testing::InSequence; 54528f6c2f2SEnji Cooper{ 54628f6c2f2SEnji Cooper InSequence seq; 54728f6c2f2SEnji Cooper 54828f6c2f2SEnji Cooper // The following are expected to occur in the order declared. 54928f6c2f2SEnji Cooper EXPECT_CALL(...); 55028f6c2f2SEnji Cooper EXPECT_CALL(...); 55128f6c2f2SEnji Cooper ... 55228f6c2f2SEnji Cooper EXPECT_CALL(...); 55328f6c2f2SEnji Cooper} 55428f6c2f2SEnji Cooper``` 55528f6c2f2SEnji Cooper 55628f6c2f2SEnji CooperThe name of the `InSequence` object does not matter. 55728f6c2f2SEnji Cooper 55828f6c2f2SEnji Cooper### Expectation {#Expectation} 55928f6c2f2SEnji Cooper 56028f6c2f2SEnji Cooper`::testing::Expectation` 56128f6c2f2SEnji Cooper 56228f6c2f2SEnji CooperRepresents a mock function call expectation as created by 56328f6c2f2SEnji Cooper[`EXPECT_CALL`](#EXPECT_CALL): 56428f6c2f2SEnji Cooper 56528f6c2f2SEnji Cooper```cpp 56628f6c2f2SEnji Cooperusing ::testing::Expectation; 56728f6c2f2SEnji CooperExpectation my_expectation = EXPECT_CALL(...); 56828f6c2f2SEnji Cooper``` 56928f6c2f2SEnji Cooper 57028f6c2f2SEnji CooperUseful for specifying sequences of expectations; see the 57128f6c2f2SEnji Cooper[`After`](#EXPECT_CALL.After) clause of `EXPECT_CALL`. 57228f6c2f2SEnji Cooper 57328f6c2f2SEnji Cooper### ExpectationSet {#ExpectationSet} 57428f6c2f2SEnji Cooper 57528f6c2f2SEnji Cooper`::testing::ExpectationSet` 57628f6c2f2SEnji Cooper 57728f6c2f2SEnji CooperRepresents a set of mock function call expectations. 57828f6c2f2SEnji Cooper 57928f6c2f2SEnji CooperUse the `+=` operator to add [`Expectation`](#Expectation) objects to the set: 58028f6c2f2SEnji Cooper 58128f6c2f2SEnji Cooper```cpp 58228f6c2f2SEnji Cooperusing ::testing::ExpectationSet; 58328f6c2f2SEnji CooperExpectationSet my_expectations; 58428f6c2f2SEnji Coopermy_expectations += EXPECT_CALL(...); 58528f6c2f2SEnji Cooper``` 58628f6c2f2SEnji Cooper 58728f6c2f2SEnji CooperUseful for specifying sequences of expectations; see the 58828f6c2f2SEnji Cooper[`After`](#EXPECT_CALL.After) clause of `EXPECT_CALL`. 589