Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
G
googletest
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Chen Yisong
googletest
Commits
dd5402d9
Commit
dd5402d9
authored
Aug 07, 2019
by
Abseil Team
Committed by
Gennadiy Civil
Aug 07, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Googletest export
Add general explanation of MOCK_METHOD, including list of supported qualifiers. PiperOrigin-RevId: 262077180
parent
79690c53
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
27 additions
and
3 deletions
+27
-3
cook_book.md
googlemock/docs/cook_book.md
+27
-3
No files found.
googlemock/docs/cook_book.md
View file @
dd5402d9
...
...
@@ -12,12 +12,36 @@ brevity, but you should do it in your own code.
### Creating Mock Classes
Mock classes are defined as normal classes, using the
`MOCK_METHOD`
macro to
generate mocked methods. The macro gets 3 or 4 parameters:
```
cpp
class
MyMock
{
public
:
MOCK_METHOD
(
ReturnType
,
MethodName
,
(
Args
...));
MOCK_METHOD
(
ReturnType
,
MethodName
,
(
Args
...),
(
Specs
...));
};
```
The first 3 parameters are simply the method declaration, split into 3 parts.
The 4th parameter accepts a closed list of qualifiers, which affect the
generated method:
*
**`const`**
- Makes the mocked method a
`const`
method. Required if
overriding a
`const`
method.
*
**`override`**
- Marks the method with
`override`
. Recommended if overriding
a
`virtual`
method.
*
**`noexcept`**
- Marks the method with
`noexcept`
. Required if overriding a
`noexcept`
method.
*
**`Calltype(...)`**
- Sets the call type for the method (e.g. to
`STDMETHODCALLTYPE`
), useful in Windows.
#### Dealing with unprotected commas
Unprotected commas, i.e. commas which are not surrounded by parentheses, prevent
`MOCK_METHOD`
from parsing its arguments correctly:
```
cpp
```
cpp
{.bad}
class MockFoo {
public:
MOCK_METHOD(std::pair<bool, int>, GetPair, ()); // Won't compile!
...
...
@@ -27,7 +51,7 @@ class MockFoo {
Solution 1 - wrap with parentheses:
```
cpp
```
cpp
{.good}
class MockFoo {
public:
MOCK_METHOD((std::pair<bool, int>), GetPair, ());
...
...
@@ -40,7 +64,7 @@ invalid C++. `MOCK_METHOD` removes the parentheses.
Solution 2 - define an alias:
```
cpp
```
cpp
{.good}
class MockFoo {
public:
using BoolAndInt = std::pair<bool, int>;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment