Abseil Team | e561358 | 2020-06-25 11:56:24 -0400 | [diff] [blame] | 1 | # gMock Cheat Sheet |
Google Code Exporter | c3d8269 | 2015-08-24 18:41:02 -0400 | [diff] [blame] | 2 | |
Abseil Team | e561358 | 2020-06-25 11:56:24 -0400 | [diff] [blame] | 3 | ## Defining a Mock Class |
Google Code Exporter | c3d8269 | 2015-08-24 18:41:02 -0400 | [diff] [blame] | 4 | |
Abseil Team | e561358 | 2020-06-25 11:56:24 -0400 | [diff] [blame] | 5 | ### Mocking a Normal Class {#MockClass} |
Google Code Exporter | c3d8269 | 2015-08-24 18:41:02 -0400 | [diff] [blame] | 6 | |
| 7 | Given |
Gennadiy Civil | 63e878b | 2019-07-17 15:35:48 -0400 | [diff] [blame] | 8 | |
Arkady Shapkin | de96759 | 2018-09-03 21:56:23 +0300 | [diff] [blame] | 9 | ```cpp |
Google Code Exporter | c3d8269 | 2015-08-24 18:41:02 -0400 | [diff] [blame] | 10 | class Foo { |
Abseil Team | 1754feb | 2022-03-21 11:56:42 -0700 | [diff] [blame] | 11 | public: |
Google Code Exporter | c3d8269 | 2015-08-24 18:41:02 -0400 | [diff] [blame] | 12 | virtual ~Foo(); |
| 13 | virtual int GetSize() const = 0; |
| 14 | virtual string Describe(const char* name) = 0; |
| 15 | virtual string Describe(int type) = 0; |
| 16 | virtual bool Process(Bar elem, int count) = 0; |
| 17 | }; |
| 18 | ``` |
Gennadiy Civil | 63e878b | 2019-07-17 15:35:48 -0400 | [diff] [blame] | 19 | |
Google Code Exporter | c3d8269 | 2015-08-24 18:41:02 -0400 | [diff] [blame] | 20 | (note that `~Foo()` **must** be virtual) we can define its mock as |
Gennadiy Civil | 63e878b | 2019-07-17 15:35:48 -0400 | [diff] [blame] | 21 | |
Arkady Shapkin | de96759 | 2018-09-03 21:56:23 +0300 | [diff] [blame] | 22 | ```cpp |
Google Code Exporter | c3d8269 | 2015-08-24 18:41:02 -0400 | [diff] [blame] | 23 | #include "gmock/gmock.h" |
| 24 | |
| 25 | class MockFoo : public Foo { |
Abseil Team | 1754feb | 2022-03-21 11:56:42 -0700 | [diff] [blame] | 26 | public: |
Gennadiy Civil | 63e878b | 2019-07-17 15:35:48 -0400 | [diff] [blame] | 27 | MOCK_METHOD(int, GetSize, (), (const, override)); |
| 28 | MOCK_METHOD(string, Describe, (const char* name), (override)); |
| 29 | MOCK_METHOD(string, Describe, (int type), (override)); |
| 30 | MOCK_METHOD(bool, Process, (Bar elem, int count), (override)); |
Google Code Exporter | c3d8269 | 2015-08-24 18:41:02 -0400 | [diff] [blame] | 31 | }; |
| 32 | ``` |
| 33 | |
Gennadiy Civil | 63e878b | 2019-07-17 15:35:48 -0400 | [diff] [blame] | 34 | To create a "nice" mock, which ignores all uninteresting calls, a "naggy" mock, |
| 35 | which warns on all uninteresting calls, or a "strict" mock, which treats them as |
| 36 | failures: |
| 37 | |
Arkady Shapkin | de96759 | 2018-09-03 21:56:23 +0300 | [diff] [blame] | 38 | ```cpp |
Gennadiy Civil | 63e878b | 2019-07-17 15:35:48 -0400 | [diff] [blame] | 39 | using ::testing::NiceMock; |
| 40 | using ::testing::NaggyMock; |
| 41 | using ::testing::StrictMock; |
| 42 | |
| 43 | NiceMock<MockFoo> nice_foo; // The type is a subclass of MockFoo. |
| 44 | NaggyMock<MockFoo> naggy_foo; // The type is a subclass of MockFoo. |
| 45 | StrictMock<MockFoo> strict_foo; // The type is a subclass of MockFoo. |
Google Code Exporter | c3d8269 | 2015-08-24 18:41:02 -0400 | [diff] [blame] | 46 | ``` |
| 47 | |
Abseil Team | d9c309f | 2021-02-18 19:18:34 -0500 | [diff] [blame] | 48 | {: .callout .note} |
Gennadiy Civil | 63e878b | 2019-07-17 15:35:48 -0400 | [diff] [blame] | 49 | **Note:** A mock object is currently naggy by default. We may make it nice by |
| 50 | default in the future. |
| 51 | |
Abseil Team | e561358 | 2020-06-25 11:56:24 -0400 | [diff] [blame] | 52 | ### Mocking a Class Template {#MockTemplate} |
Gennadiy Civil | 63e878b | 2019-07-17 15:35:48 -0400 | [diff] [blame] | 53 | |
| 54 | Class templates can be mocked just like any class. |
Google Code Exporter | c3d8269 | 2015-08-24 18:41:02 -0400 | [diff] [blame] | 55 | |
| 56 | To mock |
Gennadiy Civil | 63e878b | 2019-07-17 15:35:48 -0400 | [diff] [blame] | 57 | |
Arkady Shapkin | de96759 | 2018-09-03 21:56:23 +0300 | [diff] [blame] | 58 | ```cpp |
Google Code Exporter | c3d8269 | 2015-08-24 18:41:02 -0400 | [diff] [blame] | 59 | template <typename Elem> |
| 60 | class StackInterface { |
Abseil Team | 1754feb | 2022-03-21 11:56:42 -0700 | [diff] [blame] | 61 | public: |
Google Code Exporter | c3d8269 | 2015-08-24 18:41:02 -0400 | [diff] [blame] | 62 | virtual ~StackInterface(); |
| 63 | virtual int GetSize() const = 0; |
| 64 | virtual void Push(const Elem& x) = 0; |
| 65 | }; |
| 66 | ``` |
Gennadiy Civil | 63e878b | 2019-07-17 15:35:48 -0400 | [diff] [blame] | 67 | |
| 68 | (note that all member functions that are mocked, including `~StackInterface()` |
| 69 | **must** be virtual). |
| 70 | |
Arkady Shapkin | de96759 | 2018-09-03 21:56:23 +0300 | [diff] [blame] | 71 | ```cpp |
Google Code Exporter | c3d8269 | 2015-08-24 18:41:02 -0400 | [diff] [blame] | 72 | template <typename Elem> |
| 73 | class MockStack : public StackInterface<Elem> { |
Abseil Team | 1754feb | 2022-03-21 11:56:42 -0700 | [diff] [blame] | 74 | public: |
Gennadiy Civil | 63e878b | 2019-07-17 15:35:48 -0400 | [diff] [blame] | 75 | MOCK_METHOD(int, GetSize, (), (const, override)); |
| 76 | MOCK_METHOD(void, Push, (const Elem& x), (override)); |
Google Code Exporter | c3d8269 | 2015-08-24 18:41:02 -0400 | [diff] [blame] | 77 | }; |
| 78 | ``` |
| 79 | |
Abseil Team | e561358 | 2020-06-25 11:56:24 -0400 | [diff] [blame] | 80 | ### Specifying Calling Conventions for Mock Functions |
Google Code Exporter | c3d8269 | 2015-08-24 18:41:02 -0400 | [diff] [blame] | 81 | |
Gennadiy Civil | 63e878b | 2019-07-17 15:35:48 -0400 | [diff] [blame] | 82 | If your mock function doesn't use the default calling convention, you can |
| 83 | specify it by adding `Calltype(convention)` to `MOCK_METHOD`'s 4th parameter. |
| 84 | For example, |
| 85 | |
Arkady Shapkin | de96759 | 2018-09-03 21:56:23 +0300 | [diff] [blame] | 86 | ```cpp |
Gennadiy Civil | 63e878b | 2019-07-17 15:35:48 -0400 | [diff] [blame] | 87 | MOCK_METHOD(bool, Foo, (int n), (Calltype(STDMETHODCALLTYPE))); |
| 88 | MOCK_METHOD(int, Bar, (double x, double y), |
| 89 | (const, Calltype(STDMETHODCALLTYPE))); |
Google Code Exporter | c3d8269 | 2015-08-24 18:41:02 -0400 | [diff] [blame] | 90 | ``` |
Gennadiy Civil | 63e878b | 2019-07-17 15:35:48 -0400 | [diff] [blame] | 91 | |
Google Code Exporter | c3d8269 | 2015-08-24 18:41:02 -0400 | [diff] [blame] | 92 | where `STDMETHODCALLTYPE` is defined by `<objbase.h>` on Windows. |
| 93 | |
Abseil Team | e561358 | 2020-06-25 11:56:24 -0400 | [diff] [blame] | 94 | ## Using Mocks in Tests {#UsingMocks} |
Google Code Exporter | c3d8269 | 2015-08-24 18:41:02 -0400 | [diff] [blame] | 95 | |
Gennadiy Civil | 63e878b | 2019-07-17 15:35:48 -0400 | [diff] [blame] | 96 | The typical work flow is: |
Google Code Exporter | c3d8269 | 2015-08-24 18:41:02 -0400 | [diff] [blame] | 97 | |
Gennadiy Civil | 63e878b | 2019-07-17 15:35:48 -0400 | [diff] [blame] | 98 | 1. Import the gMock names you need to use. All gMock symbols are in the |
| 99 | `testing` namespace unless they are macros or otherwise noted. |
| 100 | 2. Create the mock objects. |
| 101 | 3. Optionally, set the default actions of the mock objects. |
| 102 | 4. Set your expectations on the mock objects (How will they be called? What |
| 103 | will they do?). |
| 104 | 5. Exercise code that uses the mock objects; if necessary, check the result |
| 105 | using googletest assertions. |
| 106 | 6. When a mock object is destructed, gMock automatically verifies that all |
| 107 | expectations on it have been satisfied. |
| 108 | |
| 109 | Here's an example: |
| 110 | |
Arkady Shapkin | de96759 | 2018-09-03 21:56:23 +0300 | [diff] [blame] | 111 | ```cpp |
| 112 | using ::testing::Return; // #1 |
Google Code Exporter | c3d8269 | 2015-08-24 18:41:02 -0400 | [diff] [blame] | 113 | |
| 114 | TEST(BarTest, DoesThis) { |
| 115 | MockFoo foo; // #2 |
| 116 | |
| 117 | ON_CALL(foo, GetSize()) // #3 |
| 118 | .WillByDefault(Return(1)); |
| 119 | // ... other default actions ... |
| 120 | |
| 121 | EXPECT_CALL(foo, Describe(5)) // #4 |
| 122 | .Times(3) |
| 123 | .WillRepeatedly(Return("Category 5")); |
| 124 | // ... other expectations ... |
| 125 | |
Abseil Team | 53cc7cd | 2021-03-22 19:35:21 -0700 | [diff] [blame] | 126 | EXPECT_EQ(MyProductionFunction(&foo), "good"); // #5 |
Google Code Exporter | c3d8269 | 2015-08-24 18:41:02 -0400 | [diff] [blame] | 127 | } // #6 |
| 128 | ``` |
| 129 | |
Abseil Team | e561358 | 2020-06-25 11:56:24 -0400 | [diff] [blame] | 130 | ## Setting Default Actions {#OnCall} |
Google Code Exporter | c3d8269 | 2015-08-24 18:41:02 -0400 | [diff] [blame] | 131 | |
Gennadiy Civil | 63e878b | 2019-07-17 15:35:48 -0400 | [diff] [blame] | 132 | gMock has a **built-in default action** for any function that returns `void`, |
| 133 | `bool`, a numeric value, or a pointer. In C++11, it will additionally returns |
| 134 | the default-constructed value, if one exists for the given type. |
Google Code Exporter | c3d8269 | 2015-08-24 18:41:02 -0400 | [diff] [blame] | 135 | |
Abseil Team | 5f6a14c | 2021-06-01 20:16:21 -0400 | [diff] [blame] | 136 | To customize the default action for functions with return type `T`, use |
| 137 | [`DefaultValue<T>`](reference/mocking.md#DefaultValue). For example: |
Gennadiy Civil | 63e878b | 2019-07-17 15:35:48 -0400 | [diff] [blame] | 138 | |
Arkady Shapkin | de96759 | 2018-09-03 21:56:23 +0300 | [diff] [blame] | 139 | ```cpp |
Gennadiy Civil | 63e878b | 2019-07-17 15:35:48 -0400 | [diff] [blame] | 140 | // Sets the default action for return type std::unique_ptr<Buzz> to |
| 141 | // creating a new Buzz every time. |
| 142 | DefaultValue<std::unique_ptr<Buzz>>::SetFactory( |
Denis Hananein | 834698c | 2022-11-20 15:08:43 +0100 | [diff] [blame] | 143 | [] { return std::make_unique<Buzz>(AccessLevel::kInternal); }); |
Gennadiy Civil | 63e878b | 2019-07-17 15:35:48 -0400 | [diff] [blame] | 144 | |
| 145 | // When this fires, the default action of MakeBuzz() will run, which |
| 146 | // will return a new Buzz object. |
| 147 | EXPECT_CALL(mock_buzzer_, MakeBuzz("hello")).Times(AnyNumber()); |
| 148 | |
| 149 | auto buzz1 = mock_buzzer_.MakeBuzz("hello"); |
| 150 | auto buzz2 = mock_buzzer_.MakeBuzz("hello"); |
Abseil Team | 53cc7cd | 2021-03-22 19:35:21 -0700 | [diff] [blame] | 151 | EXPECT_NE(buzz1, nullptr); |
| 152 | EXPECT_NE(buzz2, nullptr); |
Gennadiy Civil | 63e878b | 2019-07-17 15:35:48 -0400 | [diff] [blame] | 153 | EXPECT_NE(buzz1, buzz2); |
| 154 | |
| 155 | // Resets the default action for return type std::unique_ptr<Buzz>, |
| 156 | // to avoid interfere with other tests. |
| 157 | DefaultValue<std::unique_ptr<Buzz>>::Clear(); |
| 158 | ``` |
| 159 | |
| 160 | To customize the default action for a particular method of a specific mock |
Abseil Team | 5f6a14c | 2021-06-01 20:16:21 -0400 | [diff] [blame] | 161 | object, use [`ON_CALL`](reference/mocking.md#ON_CALL). `ON_CALL` has a similar |
| 162 | syntax to `EXPECT_CALL`, but it is used for setting default behaviors when you |
| 163 | do not require that the mock method is called. See |
| 164 | [Knowing When to Expect](gmock_cook_book.md#UseOnCall) for a more detailed |
| 165 | discussion. |
Google Code Exporter | c3d8269 | 2015-08-24 18:41:02 -0400 | [diff] [blame] | 166 | |
Abseil Team | e561358 | 2020-06-25 11:56:24 -0400 | [diff] [blame] | 167 | ## Setting Expectations {#ExpectCall} |
Google Code Exporter | c3d8269 | 2015-08-24 18:41:02 -0400 | [diff] [blame] | 168 | |
Abseil Team | 5f6a14c | 2021-06-01 20:16:21 -0400 | [diff] [blame] | 169 | See [`EXPECT_CALL`](reference/mocking.md#EXPECT_CALL) in the Mocking Reference. |
Google Code Exporter | c3d8269 | 2015-08-24 18:41:02 -0400 | [diff] [blame] | 170 | |
Abseil Team | e561358 | 2020-06-25 11:56:24 -0400 | [diff] [blame] | 171 | ## Matchers {#MatcherList} |
Google Code Exporter | c3d8269 | 2015-08-24 18:41:02 -0400 | [diff] [blame] | 172 | |
Abseil Team | 680a5aa | 2021-04-27 16:22:33 -0400 | [diff] [blame] | 173 | See the [Matchers Reference](reference/matchers.md). |
Gennadiy Civil | 63e878b | 2019-07-17 15:35:48 -0400 | [diff] [blame] | 174 | |
Abseil Team | e561358 | 2020-06-25 11:56:24 -0400 | [diff] [blame] | 175 | ## Actions {#ActionList} |
Google Code Exporter | c3d8269 | 2015-08-24 18:41:02 -0400 | [diff] [blame] | 176 | |
Abseil Team | 7e5a3a5 | 2021-05-03 22:40:25 -0400 | [diff] [blame] | 177 | See the [Actions Reference](reference/actions.md). |
Google Code Exporter | c3d8269 | 2015-08-24 18:41:02 -0400 | [diff] [blame] | 178 | |
Abseil Team | e561358 | 2020-06-25 11:56:24 -0400 | [diff] [blame] | 179 | ## Cardinalities {#CardinalityList} |
Google Code Exporter | c3d8269 | 2015-08-24 18:41:02 -0400 | [diff] [blame] | 180 | |
Abseil Team | 5f6a14c | 2021-06-01 20:16:21 -0400 | [diff] [blame] | 181 | See the [`Times` clause](reference/mocking.md#EXPECT_CALL.Times) of |
| 182 | `EXPECT_CALL` in the Mocking Reference. |
Google Code Exporter | c3d8269 | 2015-08-24 18:41:02 -0400 | [diff] [blame] | 183 | |
Abseil Team | e561358 | 2020-06-25 11:56:24 -0400 | [diff] [blame] | 184 | ## Expectation Order |
Google Code Exporter | c3d8269 | 2015-08-24 18:41:02 -0400 | [diff] [blame] | 185 | |
Abseil Team | 5f6a14c | 2021-06-01 20:16:21 -0400 | [diff] [blame] | 186 | By default, expectations can be matched in *any* order. If some or all |
| 187 | expectations must be matched in a given order, you can use the |
| 188 | [`After` clause](reference/mocking.md#EXPECT_CALL.After) or |
| 189 | [`InSequence` clause](reference/mocking.md#EXPECT_CALL.InSequence) of |
| 190 | `EXPECT_CALL`, or use an [`InSequence` object](reference/mocking.md#InSequence). |
Google Code Exporter | c3d8269 | 2015-08-24 18:41:02 -0400 | [diff] [blame] | 191 | |
Abseil Team | e561358 | 2020-06-25 11:56:24 -0400 | [diff] [blame] | 192 | ## Verifying and Resetting a Mock |
Gennadiy Civil | 63e878b | 2019-07-17 15:35:48 -0400 | [diff] [blame] | 193 | |
| 194 | gMock will verify the expectations on a mock object when it is destructed, or |
| 195 | you can do it earlier: |
| 196 | |
Arkady Shapkin | de96759 | 2018-09-03 21:56:23 +0300 | [diff] [blame] | 197 | ```cpp |
Google Code Exporter | c3d8269 | 2015-08-24 18:41:02 -0400 | [diff] [blame] | 198 | using ::testing::Mock; |
| 199 | ... |
| 200 | // Verifies and removes the expectations on mock_obj; |
Krystian Kuzniarek | 7bd4a7f | 2019-08-12 07:09:50 +0200 | [diff] [blame] | 201 | // returns true if and only if successful. |
Google Code Exporter | c3d8269 | 2015-08-24 18:41:02 -0400 | [diff] [blame] | 202 | Mock::VerifyAndClearExpectations(&mock_obj); |
| 203 | ... |
| 204 | // Verifies and removes the expectations on mock_obj; |
| 205 | // also removes the default actions set by ON_CALL(); |
Krystian Kuzniarek | 7bd4a7f | 2019-08-12 07:09:50 +0200 | [diff] [blame] | 206 | // returns true if and only if successful. |
Google Code Exporter | c3d8269 | 2015-08-24 18:41:02 -0400 | [diff] [blame] | 207 | Mock::VerifyAndClear(&mock_obj); |
| 208 | ``` |
| 209 | |
Abseil Team | eb6e927 | 2021-05-13 13:15:34 -0700 | [diff] [blame] | 210 | Do not set new expectations after verifying and clearing a mock after its use. |
| 211 | Setting expectations after code that exercises the mock has undefined behavior. |
| 212 | See [Using Mocks in Tests](gmock_for_dummies.md#using-mocks-in-tests) for more |
| 213 | information. |
| 214 | |
Gennadiy Civil | 63e878b | 2019-07-17 15:35:48 -0400 | [diff] [blame] | 215 | You can also tell gMock that a mock object can be leaked and doesn't need to be |
| 216 | verified: |
| 217 | |
Arkady Shapkin | de96759 | 2018-09-03 21:56:23 +0300 | [diff] [blame] | 218 | ```cpp |
Google Code Exporter | c3d8269 | 2015-08-24 18:41:02 -0400 | [diff] [blame] | 219 | Mock::AllowLeak(&mock_obj); |
| 220 | ``` |
| 221 | |
Abseil Team | e561358 | 2020-06-25 11:56:24 -0400 | [diff] [blame] | 222 | ## Mock Classes |
Google Code Exporter | c3d8269 | 2015-08-24 18:41:02 -0400 | [diff] [blame] | 223 | |
Gennadiy Civil | 63e878b | 2019-07-17 15:35:48 -0400 | [diff] [blame] | 224 | gMock defines a convenient mock class template |
| 225 | |
Arkady Shapkin | de96759 | 2018-09-03 21:56:23 +0300 | [diff] [blame] | 226 | ```cpp |
Google Code Exporter | c3d8269 | 2015-08-24 18:41:02 -0400 | [diff] [blame] | 227 | class MockFunction<R(A1, ..., An)> { |
| 228 | public: |
Gennadiy Civil | 63e878b | 2019-07-17 15:35:48 -0400 | [diff] [blame] | 229 | MOCK_METHOD(R, Call, (A1, ..., An)); |
Google Code Exporter | c3d8269 | 2015-08-24 18:41:02 -0400 | [diff] [blame] | 230 | }; |
| 231 | ``` |
Gennadiy Civil | 63e878b | 2019-07-17 15:35:48 -0400 | [diff] [blame] | 232 | |
dinord | 355d57d | 2021-06-22 13:30:42 +0000 | [diff] [blame] | 233 | See this [recipe](gmock_cook_book.md#UsingCheckPoints) for one application of |
Abseil Team | e3827e4 | 2021-01-22 13:49:00 -0500 | [diff] [blame] | 234 | it. |
Google Code Exporter | c3d8269 | 2015-08-24 18:41:02 -0400 | [diff] [blame] | 235 | |
Abseil Team | e561358 | 2020-06-25 11:56:24 -0400 | [diff] [blame] | 236 | ## Flags |
Google Code Exporter | c3d8269 | 2015-08-24 18:41:02 -0400 | [diff] [blame] | 237 | |
Gennadiy Civil | 63e878b | 2019-07-17 15:35:48 -0400 | [diff] [blame] | 238 | | Flag | Description | |
| 239 | | :----------------------------- | :---------------------------------------- | |
Abseil Team | 6a2adc0 | 2019-08-02 10:58:20 -0400 | [diff] [blame] | 240 | | `--gmock_catch_leaked_mocks=0` | Don't report leaked mock objects as failures. | |
| 241 | | `--gmock_verbose=LEVEL` | Sets the default verbosity level (`info`, `warning`, or `error`) of Google Mock messages. | |