blob: 2fb0403e616a79a46294a6dd5c8489f1f1dd2a78 [file] [log] [blame] [view]
Abseil Teame5613582020-06-25 11:56:24 -04001# gMock Cheat Sheet
Google Code Exporterc3d82692015-08-24 18:41:02 -04002
Abseil Teame5613582020-06-25 11:56:24 -04003## Defining a Mock Class
Google Code Exporterc3d82692015-08-24 18:41:02 -04004
Abseil Teame5613582020-06-25 11:56:24 -04005### Mocking a Normal Class {#MockClass}
Google Code Exporterc3d82692015-08-24 18:41:02 -04006
7Given
Gennadiy Civil63e878b2019-07-17 15:35:48 -04008
Arkady Shapkinde967592018-09-03 21:56:23 +03009```cpp
Google Code Exporterc3d82692015-08-24 18:41:02 -040010class Foo {
Abseil Team1754feb2022-03-21 11:56:42 -070011 public:
Google Code Exporterc3d82692015-08-24 18:41:02 -040012 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 Civil63e878b2019-07-17 15:35:48 -040019
Google Code Exporterc3d82692015-08-24 18:41:02 -040020(note that `~Foo()` **must** be virtual) we can define its mock as
Gennadiy Civil63e878b2019-07-17 15:35:48 -040021
Arkady Shapkinde967592018-09-03 21:56:23 +030022```cpp
Google Code Exporterc3d82692015-08-24 18:41:02 -040023#include "gmock/gmock.h"
24
25class MockFoo : public Foo {
Abseil Team1754feb2022-03-21 11:56:42 -070026 public:
Gennadiy Civil63e878b2019-07-17 15:35:48 -040027 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 Exporterc3d82692015-08-24 18:41:02 -040031};
32```
33
Gennadiy Civil63e878b2019-07-17 15:35:48 -040034To create a "nice" mock, which ignores all uninteresting calls, a "naggy" mock,
35which warns on all uninteresting calls, or a "strict" mock, which treats them as
36failures:
37
Arkady Shapkinde967592018-09-03 21:56:23 +030038```cpp
Gennadiy Civil63e878b2019-07-17 15:35:48 -040039using ::testing::NiceMock;
40using ::testing::NaggyMock;
41using ::testing::StrictMock;
42
43NiceMock<MockFoo> nice_foo; // The type is a subclass of MockFoo.
44NaggyMock<MockFoo> naggy_foo; // The type is a subclass of MockFoo.
45StrictMock<MockFoo> strict_foo; // The type is a subclass of MockFoo.
Google Code Exporterc3d82692015-08-24 18:41:02 -040046```
47
Abseil Teamd9c309f2021-02-18 19:18:34 -050048{: .callout .note}
Gennadiy Civil63e878b2019-07-17 15:35:48 -040049**Note:** A mock object is currently naggy by default. We may make it nice by
50default in the future.
51
Abseil Teame5613582020-06-25 11:56:24 -040052### Mocking a Class Template {#MockTemplate}
Gennadiy Civil63e878b2019-07-17 15:35:48 -040053
54Class templates can be mocked just like any class.
Google Code Exporterc3d82692015-08-24 18:41:02 -040055
56To mock
Gennadiy Civil63e878b2019-07-17 15:35:48 -040057
Arkady Shapkinde967592018-09-03 21:56:23 +030058```cpp
Google Code Exporterc3d82692015-08-24 18:41:02 -040059template <typename Elem>
60class StackInterface {
Abseil Team1754feb2022-03-21 11:56:42 -070061 public:
Google Code Exporterc3d82692015-08-24 18:41:02 -040062 virtual ~StackInterface();
63 virtual int GetSize() const = 0;
64 virtual void Push(const Elem& x) = 0;
65};
66```
Gennadiy Civil63e878b2019-07-17 15:35:48 -040067
68(note that all member functions that are mocked, including `~StackInterface()`
69**must** be virtual).
70
Arkady Shapkinde967592018-09-03 21:56:23 +030071```cpp
Google Code Exporterc3d82692015-08-24 18:41:02 -040072template <typename Elem>
73class MockStack : public StackInterface<Elem> {
Abseil Team1754feb2022-03-21 11:56:42 -070074 public:
Gennadiy Civil63e878b2019-07-17 15:35:48 -040075 MOCK_METHOD(int, GetSize, (), (const, override));
76 MOCK_METHOD(void, Push, (const Elem& x), (override));
Google Code Exporterc3d82692015-08-24 18:41:02 -040077};
78```
79
Abseil Teame5613582020-06-25 11:56:24 -040080### Specifying Calling Conventions for Mock Functions
Google Code Exporterc3d82692015-08-24 18:41:02 -040081
Gennadiy Civil63e878b2019-07-17 15:35:48 -040082If your mock function doesn't use the default calling convention, you can
83specify it by adding `Calltype(convention)` to `MOCK_METHOD`'s 4th parameter.
84For example,
85
Arkady Shapkinde967592018-09-03 21:56:23 +030086```cpp
Gennadiy Civil63e878b2019-07-17 15:35:48 -040087 MOCK_METHOD(bool, Foo, (int n), (Calltype(STDMETHODCALLTYPE)));
88 MOCK_METHOD(int, Bar, (double x, double y),
89 (const, Calltype(STDMETHODCALLTYPE)));
Google Code Exporterc3d82692015-08-24 18:41:02 -040090```
Gennadiy Civil63e878b2019-07-17 15:35:48 -040091
Google Code Exporterc3d82692015-08-24 18:41:02 -040092where `STDMETHODCALLTYPE` is defined by `<objbase.h>` on Windows.
93
Abseil Teame5613582020-06-25 11:56:24 -040094## Using Mocks in Tests {#UsingMocks}
Google Code Exporterc3d82692015-08-24 18:41:02 -040095
Gennadiy Civil63e878b2019-07-17 15:35:48 -040096The typical work flow is:
Google Code Exporterc3d82692015-08-24 18:41:02 -040097
Gennadiy Civil63e878b2019-07-17 15:35:48 -0400981. Import the gMock names you need to use. All gMock symbols are in the
99 `testing` namespace unless they are macros or otherwise noted.
1002. Create the mock objects.
1013. Optionally, set the default actions of the mock objects.
1024. Set your expectations on the mock objects (How will they be called? What
103 will they do?).
1045. Exercise code that uses the mock objects; if necessary, check the result
105 using googletest assertions.
1066. When a mock object is destructed, gMock automatically verifies that all
107 expectations on it have been satisfied.
108
109Here's an example:
110
Arkady Shapkinde967592018-09-03 21:56:23 +0300111```cpp
112using ::testing::Return; // #1
Google Code Exporterc3d82692015-08-24 18:41:02 -0400113
114TEST(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 Team53cc7cd2021-03-22 19:35:21 -0700126 EXPECT_EQ(MyProductionFunction(&foo), "good"); // #5
Google Code Exporterc3d82692015-08-24 18:41:02 -0400127} // #6
128```
129
Abseil Teame5613582020-06-25 11:56:24 -0400130## Setting Default Actions {#OnCall}
Google Code Exporterc3d82692015-08-24 18:41:02 -0400131
Gennadiy Civil63e878b2019-07-17 15:35:48 -0400132gMock 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
134the default-constructed value, if one exists for the given type.
Google Code Exporterc3d82692015-08-24 18:41:02 -0400135
Abseil Team5f6a14c2021-06-01 20:16:21 -0400136To customize the default action for functions with return type `T`, use
137[`DefaultValue<T>`](reference/mocking.md#DefaultValue). For example:
Gennadiy Civil63e878b2019-07-17 15:35:48 -0400138
Arkady Shapkinde967592018-09-03 21:56:23 +0300139```cpp
Gennadiy Civil63e878b2019-07-17 15:35:48 -0400140 // 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 Hananein834698c2022-11-20 15:08:43 +0100143 [] { return std::make_unique<Buzz>(AccessLevel::kInternal); });
Gennadiy Civil63e878b2019-07-17 15:35:48 -0400144
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 Team53cc7cd2021-03-22 19:35:21 -0700151 EXPECT_NE(buzz1, nullptr);
152 EXPECT_NE(buzz2, nullptr);
Gennadiy Civil63e878b2019-07-17 15:35:48 -0400153 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
160To customize the default action for a particular method of a specific mock
Abseil Team5f6a14c2021-06-01 20:16:21 -0400161object, use [`ON_CALL`](reference/mocking.md#ON_CALL). `ON_CALL` has a similar
162syntax to `EXPECT_CALL`, but it is used for setting default behaviors when you
163do not require that the mock method is called. See
164[Knowing When to Expect](gmock_cook_book.md#UseOnCall) for a more detailed
165discussion.
Google Code Exporterc3d82692015-08-24 18:41:02 -0400166
Abseil Teame5613582020-06-25 11:56:24 -0400167## Setting Expectations {#ExpectCall}
Google Code Exporterc3d82692015-08-24 18:41:02 -0400168
Abseil Team5f6a14c2021-06-01 20:16:21 -0400169See [`EXPECT_CALL`](reference/mocking.md#EXPECT_CALL) in the Mocking Reference.
Google Code Exporterc3d82692015-08-24 18:41:02 -0400170
Abseil Teame5613582020-06-25 11:56:24 -0400171## Matchers {#MatcherList}
Google Code Exporterc3d82692015-08-24 18:41:02 -0400172
Abseil Team680a5aa2021-04-27 16:22:33 -0400173See the [Matchers Reference](reference/matchers.md).
Gennadiy Civil63e878b2019-07-17 15:35:48 -0400174
Abseil Teame5613582020-06-25 11:56:24 -0400175## Actions {#ActionList}
Google Code Exporterc3d82692015-08-24 18:41:02 -0400176
Abseil Team7e5a3a52021-05-03 22:40:25 -0400177See the [Actions Reference](reference/actions.md).
Google Code Exporterc3d82692015-08-24 18:41:02 -0400178
Abseil Teame5613582020-06-25 11:56:24 -0400179## Cardinalities {#CardinalityList}
Google Code Exporterc3d82692015-08-24 18:41:02 -0400180
Abseil Team5f6a14c2021-06-01 20:16:21 -0400181See the [`Times` clause](reference/mocking.md#EXPECT_CALL.Times) of
182`EXPECT_CALL` in the Mocking Reference.
Google Code Exporterc3d82692015-08-24 18:41:02 -0400183
Abseil Teame5613582020-06-25 11:56:24 -0400184## Expectation Order
Google Code Exporterc3d82692015-08-24 18:41:02 -0400185
Abseil Team5f6a14c2021-06-01 20:16:21 -0400186By default, expectations can be matched in *any* order. If some or all
187expectations 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 Exporterc3d82692015-08-24 18:41:02 -0400191
Abseil Teame5613582020-06-25 11:56:24 -0400192## Verifying and Resetting a Mock
Gennadiy Civil63e878b2019-07-17 15:35:48 -0400193
194gMock will verify the expectations on a mock object when it is destructed, or
195you can do it earlier:
196
Arkady Shapkinde967592018-09-03 21:56:23 +0300197```cpp
Google Code Exporterc3d82692015-08-24 18:41:02 -0400198using ::testing::Mock;
199...
200// Verifies and removes the expectations on mock_obj;
Krystian Kuzniarek7bd4a7f2019-08-12 07:09:50 +0200201// returns true if and only if successful.
Google Code Exporterc3d82692015-08-24 18:41:02 -0400202Mock::VerifyAndClearExpectations(&mock_obj);
203...
204// Verifies and removes the expectations on mock_obj;
205// also removes the default actions set by ON_CALL();
Krystian Kuzniarek7bd4a7f2019-08-12 07:09:50 +0200206// returns true if and only if successful.
Google Code Exporterc3d82692015-08-24 18:41:02 -0400207Mock::VerifyAndClear(&mock_obj);
208```
209
Abseil Teameb6e9272021-05-13 13:15:34 -0700210Do not set new expectations after verifying and clearing a mock after its use.
211Setting expectations after code that exercises the mock has undefined behavior.
212See [Using Mocks in Tests](gmock_for_dummies.md#using-mocks-in-tests) for more
213information.
214
Gennadiy Civil63e878b2019-07-17 15:35:48 -0400215You can also tell gMock that a mock object can be leaked and doesn't need to be
216verified:
217
Arkady Shapkinde967592018-09-03 21:56:23 +0300218```cpp
Google Code Exporterc3d82692015-08-24 18:41:02 -0400219Mock::AllowLeak(&mock_obj);
220```
221
Abseil Teame5613582020-06-25 11:56:24 -0400222## Mock Classes
Google Code Exporterc3d82692015-08-24 18:41:02 -0400223
Gennadiy Civil63e878b2019-07-17 15:35:48 -0400224gMock defines a convenient mock class template
225
Arkady Shapkinde967592018-09-03 21:56:23 +0300226```cpp
Google Code Exporterc3d82692015-08-24 18:41:02 -0400227class MockFunction<R(A1, ..., An)> {
228 public:
Gennadiy Civil63e878b2019-07-17 15:35:48 -0400229 MOCK_METHOD(R, Call, (A1, ..., An));
Google Code Exporterc3d82692015-08-24 18:41:02 -0400230};
231```
Gennadiy Civil63e878b2019-07-17 15:35:48 -0400232
dinord355d57d2021-06-22 13:30:42 +0000233See this [recipe](gmock_cook_book.md#UsingCheckPoints) for one application of
Abseil Teame3827e42021-01-22 13:49:00 -0500234it.
Google Code Exporterc3d82692015-08-24 18:41:02 -0400235
Abseil Teame5613582020-06-25 11:56:24 -0400236## Flags
Google Code Exporterc3d82692015-08-24 18:41:02 -0400237
Gennadiy Civil63e878b2019-07-17 15:35:48 -0400238| Flag | Description |
239| :----------------------------- | :---------------------------------------- |
Abseil Team6a2adc02019-08-02 10:58:20 -0400240| `--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. |