gmock_cheat_sheet.md (googletest-release-1.11.0) | : | gmock_cheat_sheet.md (googletest-release-1.12.0) | ||
---|---|---|---|---|
# gMock Cheat Sheet | # gMock Cheat Sheet | |||
## Defining a Mock Class | ## Defining a Mock Class | |||
### Mocking a Normal Class {#MockClass} | ### Mocking a Normal Class {#MockClass} | |||
Given | Given | |||
```cpp | ```cpp | |||
class Foo { | class Foo { | |||
... | public: | |||
virtual ~Foo(); | virtual ~Foo(); | |||
virtual int GetSize() const = 0; | virtual int GetSize() const = 0; | |||
virtual string Describe(const char* name) = 0; | virtual string Describe(const char* name) = 0; | |||
virtual string Describe(int type) = 0; | virtual string Describe(int type) = 0; | |||
virtual bool Process(Bar elem, int count) = 0; | virtual bool Process(Bar elem, int count) = 0; | |||
}; | }; | |||
``` | ``` | |||
(note that `~Foo()` **must** be virtual) we can define its mock as | (note that `~Foo()` **must** be virtual) we can define its mock as | |||
```cpp | ```cpp | |||
#include "gmock/gmock.h" | #include "gmock/gmock.h" | |||
class MockFoo : public Foo { | class MockFoo : public Foo { | |||
... | public: | |||
MOCK_METHOD(int, GetSize, (), (const, override)); | MOCK_METHOD(int, GetSize, (), (const, override)); | |||
MOCK_METHOD(string, Describe, (const char* name), (override)); | MOCK_METHOD(string, Describe, (const char* name), (override)); | |||
MOCK_METHOD(string, Describe, (int type), (override)); | MOCK_METHOD(string, Describe, (int type), (override)); | |||
MOCK_METHOD(bool, Process, (Bar elem, int count), (override)); | MOCK_METHOD(bool, Process, (Bar elem, int count), (override)); | |||
}; | }; | |||
``` | ``` | |||
To create a "nice" mock, which ignores all uninteresting calls, a "naggy" mock, | To create a "nice" mock, which ignores all uninteresting calls, a "naggy" mock, | |||
which warns on all uninteresting calls, or a "strict" mock, which treats them as | which warns on all uninteresting calls, or a "strict" mock, which treats them as | |||
failures: | failures: | |||
skipping to change at line 61 | skipping to change at line 61 | |||
### Mocking a Class Template {#MockTemplate} | ### Mocking a Class Template {#MockTemplate} | |||
Class templates can be mocked just like any class. | Class templates can be mocked just like any class. | |||
To mock | To mock | |||
```cpp | ```cpp | |||
template <typename Elem> | template <typename Elem> | |||
class StackInterface { | class StackInterface { | |||
... | public: | |||
virtual ~StackInterface(); | virtual ~StackInterface(); | |||
virtual int GetSize() const = 0; | virtual int GetSize() const = 0; | |||
virtual void Push(const Elem& x) = 0; | virtual void Push(const Elem& x) = 0; | |||
}; | }; | |||
``` | ``` | |||
(note that all member functions that are mocked, including `~StackInterface()` | (note that all member functions that are mocked, including `~StackInterface()` | |||
**must** be virtual). | **must** be virtual). | |||
```cpp | ```cpp | |||
template <typename Elem> | template <typename Elem> | |||
class MockStack : public StackInterface<Elem> { | class MockStack : public StackInterface<Elem> { | |||
... | public: | |||
MOCK_METHOD(int, GetSize, (), (const, override)); | MOCK_METHOD(int, GetSize, (), (const, override)); | |||
MOCK_METHOD(void, Push, (const Elem& x), (override)); | MOCK_METHOD(void, Push, (const Elem& x), (override)); | |||
}; | }; | |||
``` | ``` | |||
### Specifying Calling Conventions for Mock Functions | ### Specifying Calling Conventions for Mock Functions | |||
If your mock function doesn't use the default calling convention, you can | If your mock function doesn't use the default calling convention, you can | |||
specify it by adding `Calltype(convention)` to `MOCK_METHOD`'s 4th parameter. | specify it by adding `Calltype(convention)` to `MOCK_METHOD`'s 4th parameter. | |||
For example, | For example, | |||
skipping to change at line 233 | skipping to change at line 233 | |||
gMock defines a convenient mock class template | gMock defines a convenient mock class template | |||
```cpp | ```cpp | |||
class MockFunction<R(A1, ..., An)> { | class MockFunction<R(A1, ..., An)> { | |||
public: | public: | |||
MOCK_METHOD(R, Call, (A1, ..., An)); | MOCK_METHOD(R, Call, (A1, ..., An)); | |||
}; | }; | |||
``` | ``` | |||
See this [recipe](gmock_cook_book.md#using-check-points) for one application of | See this [recipe](gmock_cook_book.md#UsingCheckPoints) for one application of | |||
it. | it. | |||
## Flags | ## Flags | |||
| Flag | Description | | | Flag | Description | | |||
| :----------------------------- | :---------------------------------------- | | | :----------------------------- | :---------------------------------------- | | |||
| `--gmock_catch_leaked_mocks=0` | Don't report leaked mock objects as failures. | | | `--gmock_catch_leaked_mocks=0` | Don't report leaked mock objects as failures. | | |||
| `--gmock_verbose=LEVEL` | Sets the default verbosity level (`info`, `warning`, or `error`) of Google Mock messages. | | | `--gmock_verbose=LEVEL` | Sets the default verbosity level (`info`, `warning`, or `error`) of Google Mock messages. | | |||
End of changes. 5 change blocks. | ||||
5 lines changed or deleted | 5 lines changed or added |