SlideShare a Scribd company logo
1 of 41
Download to read offline
8/23/13 CookBook - googlemock - Google C++ Mocking Framework Cookbook - Google C++ Mocking Framework - Google Project Hosting
https://code.google.com/p/googlemock/wiki/CookBook 1/41
Project Home Downloads Wiki Issues Source
Updated Jul 29, 2013 by w...@google.com
My favorites ▼ | Sign in
googlemock
Google C++ Mocking Framework Search projects
Search Current pages for Search
CookBook
Google C++ Mocking Framework Cookbook
Creating Mock Classes
Mocking Private or Protected Methods
Mocking Overloaded Methods
Mocking Class Templates
Mocking Nonvirtual Methods
Mocking Free Functions
The Nice, the Strict, and the Naggy
Simplifying the Interface without Breaking Existing Code
Alternative to Mocking Concrete Classes
Delegating Calls to a Fake
Delegating Calls to a Real Object
Delegating Calls to a Parent Class
Using Matchers
Matching Argument Values Exactly
Using Simple Matchers
Combining Matchers
Casting Matchers
Selecting Between Overloaded Functions
Performing Different Actions Based on the Arguments
Matching Multiple Arguments as a Whole
Using Matchers as Predicates
Using Matchers in Google Test Assertions
Using Predicates as Matchers
Matching Arguments that Are Not Copyable
Validating a Member of an Object
Validating the Value Pointed to by a Pointer Argument
Testing a Certain Property of an Object
Matching Containers
Sharing Matchers
Setting Expectations
Knowing When to Expect
Ignoring Uninteresting Calls
Disallowing Unexpected Calls
Expecting Ordered Calls
Expecting Partially Ordered Calls
Controlling When an Expectation Retires
Using Actions
Returning References from Mock Methods
Returning Live Values from Mock Methods
Combining Actions
Mocking Side Effects
Changing a Mock Object's Behavior Based on the State
Setting the Default Value for a Return Type
Setting the Default Actions for a Mock Method
Using Functions/Methods/Functors as Actions
Invoking a Function/Method/Functor Without Arguments
Invoking an Argument of the Mock Function
Ignoring an Action's Result
Selecting an Action's Arguments
8/23/13 CookBook - googlemock - Google C++ Mocking Framework Cookbook - Google C++ Mocking Framework - Google Project Hosting
https://code.google.com/p/googlemock/wiki/CookBook 2/41
Selecting an Action's Arguments
Ignoring Arguments in Action Functions
Sharing Actions
Misc Recipes on Using Google Mock
Making the Compilation Faster
Forcing a Verification
Using Check Points
Mocking Destructors
Using Google Mock and Threads
Controlling How Much Information Google Mock Prints
Gaining Super Vision into Mock Calls
Running Tests in Emacs
Fusing Google Mock Source Files
Extending Google Mock
Writing New Matchers Quickly
Writing New Parameterized Matchers Quickly
Writing New Monomorphic Matchers
Writing New Polymorphic Matchers
Writing New Cardinalities
Writing New Actions Quickly
Writing New Parameterized Actions Quickly
Restricting the Type of an Argument or Parameter in an ACTION
Writing New Action Templates Quickly
Using the ACTION Object's Type
Writing New Monomorphic Actions
Writing New Polymorphic Actions
Teaching Google Mock How to Print Your Values
You can find recipes for using Google Mock here. If you haven't yet, please read the ForDummies document first to make sure you understand
the basics.
Note: Google Mock lives in the t
e
s
t
i
n
gname space. For readability, it is recommended to write u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
F
o
o
;once in your file
before using the name F
o
odefined by Google Mock. We omit such u
s
i
n
gstatements in this page for brevity, but you should do it in your own
code.
Creating Mock Classes
Mocking Private or Protected Methods
You must always put a mock method definition (M
O
C
K
_
M
E
T
H
O
D
*
) in a p
u
b
l
i
c
:section of the mock class, regardless of the method being mocked
being p
u
b
l
i
c
, p
r
o
t
e
c
t
e
d
, or p
r
i
v
a
t
ein the base class. This allows O
N
_
C
A
L
Land E
X
P
E
C
T
_
C
A
L
Lto reference the mock function from outside of
the mock class. (Yes, C++ allows a subclass to change the access level of a virtual function in the base class.) Example:
c
l
a
s
s F
o
o {
p
u
b
l
i
c
:
.
.
.
v
i
r
t
u
a
l b
o
o
l T
r
a
n
s
f
o
r
m
(
G
a
d
g
e
t
* g
) = 0
;
p
r
o
t
e
c
t
e
d
:
v
i
r
t
u
a
l v
o
i
d R
e
s
u
m
e
(
)
;
p
r
i
v
a
t
e
:
v
i
r
t
u
a
l i
n
t G
e
t
T
i
m
e
O
u
t
(
)
;
}
;
c
l
a
s
s M
o
c
k
F
o
o : p
u
b
l
i
c F
o
o {
p
u
b
l
i
c
:
.
.
.
M
O
C
K
_
M
E
T
H
O
D
1
(
T
r
a
n
s
f
o
r
m
, b
o
o
l
(
G
a
d
g
e
t
* g
)
)
;
/
/ T
h
e f
o
l
l
o
w
i
n
g m
u
s
t b
e i
n t
h
e p
u
b
l
i
c s
e
c
t
i
o
n
, e
v
e
n t
h
o
u
g
h t
h
e
/
/ m
e
t
h
o
d
s a
r
e p
r
o
t
e
c
t
e
d o
r p
r
i
v
a
t
e i
n t
h
e b
a
s
e c
l
a
s
s
.
M
O
C
K
_
M
E
T
H
O
D
0
(
R
e
s
u
m
e
, v
o
i
d
(
)
)
;
M
O
C
K
_
M
E
T
H
O
D
0
(
G
e
t
T
i
m
e
O
u
t
, i
n
t
(
)
)
;
}
;
Mocking Overloaded Methods
8/23/13 CookBook - googlemock - Google C++ Mocking Framework Cookbook - Google C++ Mocking Framework - Google Project Hosting
https://code.google.com/p/googlemock/wiki/CookBook 3/41
You can mock overloaded functions as usual. No special attention is required:
c
l
a
s
s F
o
o {
.
.
.
/
/ M
u
s
t b
e v
i
r
t
u
a
l a
s w
e
'
l
l i
n
h
e
r
i
t f
r
o
m F
o
o
.
v
i
r
t
u
a
l ~
F
o
o
(
)
;
/
/ O
v
e
r
l
o
a
d
e
d o
n t
h
e t
y
p
e
s a
n
d
/
o
r n
u
m
b
e
r
s o
f a
r
g
u
m
e
n
t
s
.
v
i
r
t
u
a
l i
n
t A
d
d
(
E
l
e
m
e
n
t x
)
;
v
i
r
t
u
a
l i
n
t A
d
d
(
i
n
t t
i
m
e
s
, E
l
e
m
e
n
t x
)
;
/
/ O
v
e
r
l
o
a
d
e
d o
n t
h
e c
o
n
s
t
-
n
e
s
s o
f t
h
i
s o
b
j
e
c
t
.
v
i
r
t
u
a
l B
a
r
& G
e
t
B
a
r
(
)
;
v
i
r
t
u
a
l c
o
n
s
t B
a
r
& G
e
t
B
a
r
(
) c
o
n
s
t
;
}
;
c
l
a
s
s M
o
c
k
F
o
o : p
u
b
l
i
c F
o
o {
.
.
.
M
O
C
K
_
M
E
T
H
O
D
1
(
A
d
d
, i
n
t
(
E
l
e
m
e
n
t x
)
)
;
M
O
C
K
_
M
E
T
H
O
D
2
(
A
d
d
, i
n
t
(
i
n
t t
i
m
e
s
, E
l
e
m
e
n
t x
)
;
M
O
C
K
_
M
E
T
H
O
D
0
(
G
e
t
B
a
r
, B
a
r
&
(
)
)
;
M
O
C
K
_
C
O
N
S
T
_
M
E
T
H
O
D
0
(
G
e
t
B
a
r
, c
o
n
s
t B
a
r
&
(
)
)
;
}
;
Note: if you don't mock all versions of the overloaded method, the compiler will give you a warning about some methods in the base class being
hidden. To fix that, use u
s
i
n
gto bring them in scope:
c
l
a
s
s M
o
c
k
F
o
o : p
u
b
l
i
c F
o
o {
.
.
.
u
s
i
n
g F
o
o
:
:
A
d
d
;
M
O
C
K
_
M
E
T
H
O
D
1
(
A
d
d
, i
n
t
(
E
l
e
m
e
n
t x
)
)
;
/
/ W
e d
o
n
'
t w
a
n
t t
o m
o
c
k i
n
t A
d
d
(
i
n
t t
i
m
e
s
, E
l
e
m
e
n
t x
)
;
.
.
.
}
;
Mocking Class Templates
To mock a class template, append _
Tto the M
O
C
K
_
*macros:
t
e
m
p
l
a
t
e <
t
y
p
e
n
a
m
e E
l
e
m
>
c
l
a
s
s S
t
a
c
k
I
n
t
e
r
f
a
c
e {
.
.
.
/
/ M
u
s
t b
e v
i
r
t
u
a
l a
s w
e
'
l
l i
n
h
e
r
i
t f
r
o
m S
t
a
c
k
I
n
t
e
r
f
a
c
e
.
v
i
r
t
u
a
l ~
S
t
a
c
k
I
n
t
e
r
f
a
c
e
(
)
;
v
i
r
t
u
a
l i
n
t G
e
t
S
i
z
e
(
) c
o
n
s
t = 0
;
v
i
r
t
u
a
l v
o
i
d P
u
s
h
(
c
o
n
s
t E
l
e
m
& x
) = 0
;
}
;
t
e
m
p
l
a
t
e <
t
y
p
e
n
a
m
e E
l
e
m
>
c
l
a
s
s M
o
c
k
S
t
a
c
k : p
u
b
l
i
c S
t
a
c
k
I
n
t
e
r
f
a
c
e
<
E
l
e
m
> {
.
.
.
M
O
C
K
_
C
O
N
S
T
_
M
E
T
H
O
D
0
_
T
(
G
e
t
S
i
z
e
, i
n
t
(
)
)
;
M
O
C
K
_
M
E
T
H
O
D
1
_
T
(
P
u
s
h
, v
o
i
d
(
c
o
n
s
t E
l
e
m
& x
)
)
;
}
;
Mocking Nonvirtual Methods
Google Mock can mock non­virtual functions to be used in what we call hi­perf dependency injection.
In this case, instead of sharing a common base class with the real class, your mock class will be unrelated to the real class, but contain
methods with the same signatures. The syntax for mocking non­virtual methods is the same as mocking virtual methods:
/
/ A s
i
m
p
l
e p
a
c
k
e
t s
t
r
e
a
m c
l
a
s
s
. N
o
n
e o
f i
t
s m
e
m
b
e
r
s i
s v
i
r
t
u
a
l
.
c
l
a
s
s C
o
n
c
r
e
t
e
P
a
c
k
e
t
S
t
r
e
a
m {
p
u
b
l
i
c
:
v
o
i
d A
p
p
e
n
d
P
a
c
k
e
t
(
P
a
c
k
e
t
* n
e
w
_
p
a
c
k
e
t
)
;
c
o
n
s
t P
a
c
k
e
t
* G
e
t
P
a
c
k
e
t
(
s
i
z
e
_
t p
a
c
k
e
t
_
n
u
m
b
e
r
) c
o
n
s
t
;
s
i
z
e
_
t N
u
m
b
e
r
O
f
P
a
c
k
e
t
s
(
) c
o
n
s
t
;
.
.
.
8/23/13 CookBook - googlemock - Google C++ Mocking Framework Cookbook - Google C++ Mocking Framework - Google Project Hosting
https://code.google.com/p/googlemock/wiki/CookBook 4/41
.
.
.
}
;
/
/ A m
o
c
k p
a
c
k
e
t s
t
r
e
a
m c
l
a
s
s
. I
t i
n
h
e
r
i
t
s f
r
o
m n
o o
t
h
e
r
, b
u
t d
e
f
i
n
e
s
/
/ G
e
t
P
a
c
k
e
t
(
) a
n
d N
u
m
b
e
r
O
f
P
a
c
k
e
t
s
(
)
.
c
l
a
s
s M
o
c
k
P
a
c
k
e
t
S
t
r
e
a
m {
p
u
b
l
i
c
:
M
O
C
K
_
C
O
N
S
T
_
M
E
T
H
O
D
1
(
G
e
t
P
a
c
k
e
t
, c
o
n
s
t P
a
c
k
e
t
*
(
s
i
z
e
_
t p
a
c
k
e
t
_
n
u
m
b
e
r
)
)
;
M
O
C
K
_
C
O
N
S
T
_
M
E
T
H
O
D
0
(
N
u
m
b
e
r
O
f
P
a
c
k
e
t
s
, s
i
z
e
_
t
(
)
)
;
.
.
.
}
;
Note that the mock class doesn't define A
p
p
e
n
d
P
a
c
k
e
t
(
)
, unlike the real class. That's fine as long as the test doesn't need to call it.
Next, you need a way to say that you want to use C
o
n
c
r
e
t
e
P
a
c
k
e
t
S
t
r
e
a
min production code, and use M
o
c
k
P
a
c
k
e
t
S
t
r
e
a
min tests. Since the
functions are not virtual and the two classes are unrelated, you must specify your choice at compile time (as opposed to run time).
One way to do it is to templatize your code that needs to use a packet stream. More specifically, you will give your code a template type
argument for the type of the packet stream. In production, you will instantiate your template with C
o
n
c
r
e
t
e
P
a
c
k
e
t
S
t
r
e
a
mas the type argument.
In tests, you will instantiate the same template with M
o
c
k
P
a
c
k
e
t
S
t
r
e
a
m
. For example, you may write:
t
e
m
p
l
a
t
e <
c
l
a
s
s P
a
c
k
e
t
S
t
r
e
a
m
>
v
o
i
d C
r
e
a
t
e
C
o
n
n
e
c
t
i
o
n
(
P
a
c
k
e
t
S
t
r
e
a
m
* s
t
r
e
a
m
) { .
.
. }
t
e
m
p
l
a
t
e <
c
l
a
s
s P
a
c
k
e
t
S
t
r
e
a
m
>
c
l
a
s
s P
a
c
k
e
t
R
e
a
d
e
r {
p
u
b
l
i
c
:
v
o
i
d R
e
a
d
P
a
c
k
e
t
s
(
P
a
c
k
e
t
S
t
r
e
a
m
* s
t
r
e
a
m
, s
i
z
e
_
t p
a
c
k
e
t
_
n
u
m
)
;
}
;
Then you can use C
r
e
a
t
e
C
o
n
n
e
c
t
i
o
n
<
C
o
n
c
r
e
t
e
P
a
c
k
e
t
S
t
r
e
a
m
>
(
)and P
a
c
k
e
t
R
e
a
d
e
r
<
C
o
n
c
r
e
t
e
P
a
c
k
e
t
S
t
r
e
a
m
>in production code, and use
C
r
e
a
t
e
C
o
n
n
e
c
t
i
o
n
<
M
o
c
k
P
a
c
k
e
t
S
t
r
e
a
m
>
(
)and P
a
c
k
e
t
R
e
a
d
e
r
<
M
o
c
k
P
a
c
k
e
t
S
t
r
e
a
m
>in tests.
M
o
c
k
P
a
c
k
e
t
S
t
r
e
a
m m
o
c
k
_
s
t
r
e
a
m
;
E
X
P
E
C
T
_
C
A
L
L
(
m
o
c
k
_
s
t
r
e
a
m
, .
.
.
)
.
.
.
;
.
. s
e
t m
o
r
e e
x
p
e
c
t
a
t
i
o
n
s o
n m
o
c
k
_
s
t
r
e
a
m .
.
.
P
a
c
k
e
t
R
e
a
d
e
r
<
M
o
c
k
P
a
c
k
e
t
S
t
r
e
a
m
> r
e
a
d
e
r
(
&
m
o
c
k
_
s
t
r
e
a
m
)
;
.
.
. e
x
e
r
c
i
s
e r
e
a
d
e
r .
.
.
Mocking Free Functions
It's possible to use Google Mock to mock a free function (i.e. a C­style function or a static method). You just need to rewrite your code to use an
interface (abstract class).
Instead of calling a free function (say, O
p
e
n
F
i
l
e
) directly, introduce an interface for it and have a concrete subclass that calls the free function:
c
l
a
s
s F
i
l
e
I
n
t
e
r
f
a
c
e {
p
u
b
l
i
c
:
.
.
.
v
i
r
t
u
a
l b
o
o
l O
p
e
n
(
c
o
n
s
t c
h
a
r
* p
a
t
h
, c
o
n
s
t c
h
a
r
* m
o
d
e
) = 0
;
}
;
c
l
a
s
s F
i
l
e : p
u
b
l
i
c F
i
l
e
I
n
t
e
r
f
a
c
e {
p
u
b
l
i
c
:
.
.
.
v
i
r
t
u
a
l b
o
o
l O
p
e
n
(
c
o
n
s
t c
h
a
r
* p
a
t
h
, c
o
n
s
t c
h
a
r
* m
o
d
e
) {
r
e
t
u
r
n O
p
e
n
F
i
l
e
(
p
a
t
h
, m
o
d
e
)
;
}
}
;
Your code should talk to F
i
l
e
I
n
t
e
r
f
a
c
eto open a file. Now it's easy to mock out the function.
This may seem much hassle, but in practice you often have multiple related functions that you can put in the same interface, so the per­function
syntactic overhead will be much lower.
If you are concerned about the performance overhead incurred by virtual functions, and profiling confirms your concern, you can combine this
with the recipe for mocking non­virtual methods.
The Nice, the Strict, and the Naggy
If a mock method has no E
X
P
E
C
T
_
C
A
L
Lspec but is called, Google Mock will print a warning about the "uninteresting call". The rationale is:
8/23/13 CookBook - googlemock - Google C++ Mocking Framework Cookbook - Google C++ Mocking Framework - Google Project Hosting
https://code.google.com/p/googlemock/wiki/CookBook 5/41
New methods may be added to an interface after a test is written. We shouldn't fail a test just because a method it doesn't know about is
called.
However, this may also mean there's a bug in the test, so Google Mock shouldn't be silent either. If the user believes these calls are
harmless, he can add an E
X
P
E
C
T
_
C
A
L
L
(
)to suppress the warning.
However, sometimes you may want to suppress all "uninteresting call" warnings, while sometimes you may want the opposite, i.e. to treat all of
them as errors. Google Mock lets you make the decision on a per­mock­object basis.
Suppose your test uses a mock class M
o
c
k
F
o
o
:
T
E
S
T
(
.
.
.
) {
M
o
c
k
F
o
o m
o
c
k
_
f
o
o
;
E
X
P
E
C
T
_
C
A
L
L
(
m
o
c
k
_
f
o
o
, D
o
T
h
i
s
(
)
)
;
.
.
. c
o
d
e t
h
a
t u
s
e
s m
o
c
k
_
f
o
o .
.
.
}
If a method of m
o
c
k
_
f
o
oother than D
o
T
h
i
s
(
)is called, it will be reported by Google Mock as a warning. However, if you rewrite your test to use
N
i
c
e
M
o
c
k
<
M
o
c
k
F
o
o
>instead, the warning will be gone, resulting in a cleaner test output:
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
N
i
c
e
M
o
c
k
;
T
E
S
T
(
.
.
.
) {
N
i
c
e
M
o
c
k
<
M
o
c
k
F
o
o
> m
o
c
k
_
f
o
o
;
E
X
P
E
C
T
_
C
A
L
L
(
m
o
c
k
_
f
o
o
, D
o
T
h
i
s
(
)
)
;
.
.
. c
o
d
e t
h
a
t u
s
e
s m
o
c
k
_
f
o
o .
.
.
}
N
i
c
e
M
o
c
k
<
M
o
c
k
F
o
o
>is a subclass of M
o
c
k
F
o
o
, so it can be used wherever M
o
c
k
F
o
ois accepted.
It also works if M
o
c
k
F
o
o
's constructor takes some arguments, as N
i
c
e
M
o
c
k
<
M
o
c
k
F
o
o
>"inherits" M
o
c
k
F
o
o
's constructors:
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
N
i
c
e
M
o
c
k
;
T
E
S
T
(
.
.
.
) {
N
i
c
e
M
o
c
k
<
M
o
c
k
F
o
o
> m
o
c
k
_
f
o
o
(
5
, "
h
i
"
)
; /
/ C
a
l
l
s M
o
c
k
F
o
o
(
5
, "
h
i
"
)
.
E
X
P
E
C
T
_
C
A
L
L
(
m
o
c
k
_
f
o
o
, D
o
T
h
i
s
(
)
)
;
.
.
. c
o
d
e t
h
a
t u
s
e
s m
o
c
k
_
f
o
o .
.
.
}
The usage of S
t
r
i
c
t
M
o
c
kis similar, except that it makes all uninteresting calls failures:
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
S
t
r
i
c
t
M
o
c
k
;
T
E
S
T
(
.
.
.
) {
S
t
r
i
c
t
M
o
c
k
<
M
o
c
k
F
o
o
> m
o
c
k
_
f
o
o
;
E
X
P
E
C
T
_
C
A
L
L
(
m
o
c
k
_
f
o
o
, D
o
T
h
i
s
(
)
)
;
.
.
. c
o
d
e t
h
a
t u
s
e
s m
o
c
k
_
f
o
o .
.
.
/
/ T
h
e t
e
s
t w
i
l
l f
a
i
l i
f a m
e
t
h
o
d o
f m
o
c
k
_
f
o
o o
t
h
e
r t
h
a
n D
o
T
h
i
s
(
)
/
/ i
s c
a
l
l
e
d
.
}
There are some caveats though (I don't like them just as much as the next guy, but sadly they are side effects of C++'s limitations):
1. N
i
c
e
M
o
c
k
<
M
o
c
k
F
o
o
>and S
t
r
i
c
t
M
o
c
k
<
M
o
c
k
F
o
o
>only work for mock methods defined using the M
O
C
K
_
M
E
T
H
O
D
*family of macros directly in
the M
o
c
k
F
o
oclass. If a mock method is defined in a base class of M
o
c
k
F
o
o
, the "nice" or "strict" modifier may not affect it, depending on
the compiler. In particular, nesting N
i
c
e
M
o
c
kand S
t
r
i
c
t
M
o
c
k(e.g. N
i
c
e
M
o
c
k
<
S
t
r
i
c
t
M
o
c
k
<
M
o
c
k
F
o
o
> >
) is not supported.
2. The constructors of the base mock (M
o
c
k
F
o
o
) cannot have arguments passed by non­const reference, which happens to be banned by the
Google C++ style guide.
3. During the constructor or destructor of M
o
c
k
F
o
o
, the mock object is not nice or strict. This may cause surprises if the constructor or
destructor calls a mock method on t
h
i
sobject. (This behavior, however, is consistent with C++'s general rule: if a constructor or destructor
calls a virtual method of t
h
i
sobject, that method is treated as non­virtual. In other words, to the base class's constructor or destructor,
t
h
i
sobject behaves like an instance of the base class, not the derived class. This rule is required for safety. Otherwise a base constructor
may use members of a derived class before they are initialized, or a base destructor may use members of a derived class after they have
been destroyed.)
Finally, you should be very cautious about when to use naggy or strict mocks, as they tend to make tests more brittle and harder to maintain.
When you refactor your code without changing its externally visible behavior, ideally you should't need to update any tests. If your code interacts
with a naggy mock, however, you may start to get spammed with warnings as the result of your change. Worse, if your code interacts with a
strict mock, your tests may start to fail and you'll be forced to fix them. Our general recommendation is to use nice mocks (not yet the default)
most of the time, use naggy mocks (the current default) when developing or debugging tests, and use strict mocks only as the last resort.
8/23/13 CookBook - googlemock - Google C++ Mocking Framework Cookbook - Google C++ Mocking Framework - Google Project Hosting
https://code.google.com/p/googlemock/wiki/CookBook 6/41
Simplifying the Interface without Breaking Existing Code
Sometimes a method has a long list of arguments that is mostly uninteresting. For example,
c
l
a
s
s L
o
g
S
i
n
k {
p
u
b
l
i
c
:
.
.
.
v
i
r
t
u
a
l v
o
i
d s
e
n
d
(
L
o
g
S
e
v
e
r
i
t
y s
e
v
e
r
i
t
y
, c
o
n
s
t c
h
a
r
* f
u
l
l
_
f
i
l
e
n
a
m
e
,
c
o
n
s
t c
h
a
r
* b
a
s
e
_
f
i
l
e
n
a
m
e
, i
n
t l
i
n
e
,
c
o
n
s
t s
t
r
u
c
t t
m
* t
m
_
t
i
m
e
,
c
o
n
s
t c
h
a
r
* m
e
s
s
a
g
e
, s
i
z
e
_
t m
e
s
s
a
g
e
_
l
e
n
) = 0
;
}
;
This method's argument list is lengthy and hard to work with (let's say that the m
e
s
s
a
g
eargument is not even 0­terminated). If we mock it as is,
using the mock will be awkward. If, however, we try to simplify this interface, we'll need to fix all clients depending on it, which is often
infeasible.
The trick is to re­dispatch the method in the mock class:
c
l
a
s
s S
c
o
p
e
d
M
o
c
k
L
o
g : p
u
b
l
i
c L
o
g
S
i
n
k {
p
u
b
l
i
c
:
.
.
.
v
i
r
t
u
a
l v
o
i
d s
e
n
d
(
L
o
g
S
e
v
e
r
i
t
y s
e
v
e
r
i
t
y
, c
o
n
s
t c
h
a
r
* f
u
l
l
_
f
i
l
e
n
a
m
e
,
c
o
n
s
t c
h
a
r
* b
a
s
e
_
f
i
l
e
n
a
m
e
, i
n
t l
i
n
e
, c
o
n
s
t t
m
* t
m
_
t
i
m
e
,
c
o
n
s
t c
h
a
r
* m
e
s
s
a
g
e
, s
i
z
e
_
t m
e
s
s
a
g
e
_
l
e
n
) {
/
/ W
e a
r
e o
n
l
y i
n
t
e
r
e
s
t
e
d i
n t
h
e l
o
g s
e
v
e
r
i
t
y
, f
u
l
l f
i
l
e n
a
m
e
, a
n
d
/
/ l
o
g m
e
s
s
a
g
e
.
L
o
g
(
s
e
v
e
r
i
t
y
, f
u
l
l
_
f
i
l
e
n
a
m
e
, s
t
d
:
:
s
t
r
i
n
g
(
m
e
s
s
a
g
e
, m
e
s
s
a
g
e
_
l
e
n
)
)
;
}
/
/ I
m
p
l
e
m
e
n
t
s t
h
e m
o
c
k m
e
t
h
o
d
:
/
/
/
/ v
o
i
d L
o
g
(
L
o
g
S
e
v
e
r
i
t
y s
e
v
e
r
i
t
y
,
/
/ c
o
n
s
t s
t
r
i
n
g
& f
i
l
e
_
p
a
t
h
,
/
/ c
o
n
s
t s
t
r
i
n
g
& m
e
s
s
a
g
e
)
;
M
O
C
K
_
M
E
T
H
O
D
3
(
L
o
g
, v
o
i
d
(
L
o
g
S
e
v
e
r
i
t
y s
e
v
e
r
i
t
y
, c
o
n
s
t s
t
r
i
n
g
& f
i
l
e
_
p
a
t
h
,
c
o
n
s
t s
t
r
i
n
g
& m
e
s
s
a
g
e
)
)
;
}
;
By defining a new mock method with a trimmed argument list, we make the mock class much more user­friendly.
Alternative to Mocking Concrete Classes
Often you may find yourself using classes that don't implement interfaces. In order to test your code that uses such a class (let's call it
C
o
n
c
r
e
t
e
), you may be tempted to make the methods of C
o
n
c
r
e
t
evirtual and then mock it.
Try not to do that.
Making a non­virtual function virtual is a big decision. It creates an extension point where subclasses can tweak your class' behavior. This
weakens your control on the class because now it's harder to maintain the class' invariants. You should make a function virtual only when there
is a valid reason for a subclass to override it.
Mocking concrete classes directly is problematic as it creates a tight coupling between the class and the tests ­ any small change in the class
may invalidate your tests and make test maintenance a pain.
To avoid such problems, many programmers have been practicing "coding to interfaces": instead of talking to the C
o
n
c
r
e
t
eclass, your code
would define an interface and talk to it. Then you implement that interface as an adaptor on top of C
o
n
c
r
e
t
e
. In tests, you can easily mock that
interface to observe how your code is doing.
This technique incurs some overhead:
You pay the cost of virtual function calls (usually not a problem).
There is more abstraction for the programmers to learn.
However, it can also bring significant benefits in addition to better testability:
C
o
n
c
r
e
t
e
's API may not fit your problem domain very well, as you may not be the only client it tries to serve. By designing your own
interface, you have a chance to tailor it to your need ­ you may add higher­level functionalities, rename stuff, etc instead of just trimming the
class. This allows you to write your code (user of the interface) in a more natural way, which means it will be more readable, more
maintainable, and you'll be more productive.
If C
o
n
c
r
e
t
e
's implementation ever has to change, you don't have to rewrite everywhere it is used. Instead, you can absorb the change in
your implementation of the interface, and your other code and tests will be insulated from this change.
8/23/13 CookBook - googlemock - Google C++ Mocking Framework Cookbook - Google C++ Mocking Framework - Google Project Hosting
https://code.google.com/p/googlemock/wiki/CookBook 7/41
Some people worry that if everyone is practicing this technique, they will end up writing lots of redundant code. This concern is totally
understandable. However, there are two reasons why it may not be the case:
Different projects may need to use C
o
n
c
r
e
t
ein different ways, so the best interfaces for them will be different. Therefore, each of them will
have its own domain­specific interface on top of C
o
n
c
r
e
t
e
, and they will not be the same code.
If enough projects want to use the same interface, they can always share it, just like they have been sharing C
o
n
c
r
e
t
e
. You can check in
the interface and the adaptor somewhere near C
o
n
c
r
e
t
e(perhaps in a c
o
n
t
r
i
bsub­directory) and let many projects use it.
You need to weigh the pros and cons carefully for your particular problem, but I'd like to assure you that the Java community has been practicing
this for a long time and it's a proven effective technique applicable in a wide variety of situations. :­)
Delegating Calls to a Fake
Some times you have a non­trivial fake implementation of an interface. For example:
c
l
a
s
s F
o
o {
p
u
b
l
i
c
:
v
i
r
t
u
a
l ~
F
o
o
(
) {
}
v
i
r
t
u
a
l c
h
a
r D
o
T
h
i
s
(
i
n
t n
) = 0
;
v
i
r
t
u
a
l v
o
i
d D
o
T
h
a
t
(
c
o
n
s
t c
h
a
r
* s
, i
n
t
* p
) = 0
;
}
;
c
l
a
s
s F
a
k
e
F
o
o : p
u
b
l
i
c F
o
o {
p
u
b
l
i
c
:
v
i
r
t
u
a
l c
h
a
r D
o
T
h
i
s
(
i
n
t n
) {
r
e
t
u
r
n (
n > 0
) ? '
+
' :
(
n < 0
) ? '
-
' : '
0
'
;
}
v
i
r
t
u
a
l v
o
i
d D
o
T
h
a
t
(
c
o
n
s
t c
h
a
r
* s
, i
n
t
* p
) {
*
p = s
t
r
l
e
n
(
s
)
;
}
}
;
Now you want to mock this interface such that you can set expectations on it. However, you also want to use F
a
k
e
F
o
ofor the default behavior,
as duplicating it in the mock object is, well, a lot of work.
When you define the mock class using Google Mock, you can have it delegate its default action to a fake class you already have, using this
pattern:
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
_
;
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
I
n
v
o
k
e
;
c
l
a
s
s M
o
c
k
F
o
o : p
u
b
l
i
c F
o
o {
p
u
b
l
i
c
:
/
/ N
o
r
m
a
l m
o
c
k m
e
t
h
o
d d
e
f
i
n
i
t
i
o
n
s u
s
i
n
g G
o
o
g
l
e M
o
c
k
.
M
O
C
K
_
M
E
T
H
O
D
1
(
D
o
T
h
i
s
, c
h
a
r
(
i
n
t n
)
)
;
M
O
C
K
_
M
E
T
H
O
D
2
(
D
o
T
h
a
t
, v
o
i
d
(
c
o
n
s
t c
h
a
r
* s
, i
n
t
* p
)
)
;
/
/ D
e
l
e
g
a
t
e
s t
h
e d
e
f
a
u
l
t a
c
t
i
o
n
s o
f t
h
e m
e
t
h
o
d
s t
o a F
a
k
e
F
o
o o
b
j
e
c
t
.
/
/ T
h
i
s m
u
s
t b
e c
a
l
l
e
d *
b
e
f
o
r
e
* t
h
e c
u
s
t
o
m O
N
_
C
A
L
L
(
) s
t
a
t
e
m
e
n
t
s
.
v
o
i
d D
e
l
e
g
a
t
e
T
o
F
a
k
e
(
) {
O
N
_
C
A
L
L
(
*
t
h
i
s
, D
o
T
h
i
s
(
_
)
)
.
W
i
l
l
B
y
D
e
f
a
u
l
t
(
I
n
v
o
k
e
(
&
f
a
k
e
_
, &
F
a
k
e
F
o
o
:
:
D
o
T
h
i
s
)
)
;
O
N
_
C
A
L
L
(
*
t
h
i
s
, D
o
T
h
a
t
(
_
, _
)
)
.
W
i
l
l
B
y
D
e
f
a
u
l
t
(
I
n
v
o
k
e
(
&
f
a
k
e
_
, &
F
a
k
e
F
o
o
:
:
D
o
T
h
a
t
)
)
;
}
p
r
i
v
a
t
e
:
F
a
k
e
F
o
o f
a
k
e
_
; /
/ K
e
e
p
s a
n i
n
s
t
a
n
c
e o
f t
h
e f
a
k
e i
n t
h
e m
o
c
k
.
}
;
With that, you can use M
o
c
k
F
o
oin your tests as usual. Just remember that if you don't explicitly set an action in an O
N
_
C
A
L
L
(
)or
E
X
P
E
C
T
_
C
A
L
L
(
)
, the fake will be called upon to do it:
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
_
;
T
E
S
T
(
A
b
c
T
e
s
t
, X
y
z
) {
M
o
c
k
F
o
o f
o
o
;
f
o
o
.
D
e
l
e
g
a
t
e
T
o
F
a
k
e
(
)
; /
/ E
n
a
b
l
e
s t
h
e f
a
k
e f
o
r d
e
l
e
g
a
t
i
o
n
.
/
/ P
u
t y
o
u
r O
N
_
C
A
L
L
(
f
o
o
, .
.
.
)
s h
e
r
e
, i
f a
n
y
.
8/23/13 CookBook - googlemock - Google C++ Mocking Framework Cookbook - Google C++ Mocking Framework - Google Project Hosting
https://code.google.com/p/googlemock/wiki/CookBook 8/41
/
/ N
o a
c
t
i
o
n s
p
e
c
i
f
i
e
d
, m
e
a
n
i
n
g t
o u
s
e t
h
e d
e
f
a
u
l
t a
c
t
i
o
n
.
E
X
P
E
C
T
_
C
A
L
L
(
f
o
o
, D
o
T
h
i
s
(
5
)
)
;
E
X
P
E
C
T
_
C
A
L
L
(
f
o
o
, D
o
T
h
a
t
(
_
, _
)
)
;
i
n
t n = 0
;
E
X
P
E
C
T
_
E
Q
(
'
+
'
, f
o
o
.
D
o
T
h
i
s
(
5
)
)
; /
/ F
a
k
e
F
o
o
:
:
D
o
T
h
i
s
(
) i
s i
n
v
o
k
e
d
.
f
o
o
.
D
o
T
h
a
t
(
"
H
i
"
, &
n
)
; /
/ F
a
k
e
F
o
o
:
:
D
o
T
h
a
t
(
) i
s i
n
v
o
k
e
d
.
E
X
P
E
C
T
_
E
Q
(
2
, n
)
;
}
Some tips:
If you want, you can still override the default action by providing your own O
N
_
C
A
L
L
(
)or using .
W
i
l
l
O
n
c
e
(
)/ .
W
i
l
l
R
e
p
e
a
t
e
d
l
y
(
)in
E
X
P
E
C
T
_
C
A
L
L
(
)
.
In D
e
l
e
g
a
t
e
T
o
F
a
k
e
(
)
, you only need to delegate the methods whose fake implementation you intend to use.
The general technique discussed here works for overloaded methods, but you'll need to tell the compiler which version you mean. To
disambiguate a mock function (the one you specify inside the parentheses of O
N
_
C
A
L
L
(
)
), see the "Selecting Between Overloaded
Functions" section on this page; to disambiguate a fake function (the one you place inside I
n
v
o
k
e
(
)
), use a s
t
a
t
i
c
_
c
a
s
tto specify the
function's type. For instance, if class F
o
ohas methods c
h
a
r D
o
T
h
i
s
(
i
n
t n
)and b
o
o
l D
o
T
h
i
s
(
d
o
u
b
l
e x
) c
o
n
s
t
, and you want to invoke
the latter, you need to write I
n
v
o
k
e
(
&
f
a
k
e
_
, s
t
a
t
i
c
_
c
a
s
t
<
b
o
o
l (
F
a
k
e
F
o
o
:
:
*
)
(
d
o
u
b
l
e
) c
o
n
s
t
>
(
&
F
a
k
e
F
o
o
:
:
D
o
T
h
i
s
)
)instead of
I
n
v
o
k
e
(
&
f
a
k
e
_
, &
F
a
k
e
F
o
o
:
:
D
o
T
h
i
s
)(The strange­looking thing inside the angled brackets of s
t
a
t
i
c
_
c
a
s
tis the type of a function
pointer to the second D
o
T
h
i
s
(
)method.).
Having to mix a mock and a fake is often a sign of something gone wrong. Perhaps you haven't got used to the interaction­based way of
testing yet. Or perhaps your interface is taking on too many roles and should be split up. Therefore, don't abuse this. We would only
recommend to do it as an intermediate step when you are refactoring your code.
Regarding the tip on mixing a mock and a fake, here's an example on why it may be a bad sign: Suppose you have a class S
y
s
t
e
mfor low­level
system operations. In particular, it does file and I/O operations. And suppose you want to test how your code uses S
y
s
t
e
mto do I/O, and you
just want the file operations to work normally. If you mock out the entire S
y
s
t
e
mclass, you'll have to provide a fake implementation for the file
operation part, which suggests that S
y
s
t
e
mis taking on too many roles.
Instead, you can define a F
i
l
e
O
p
sinterface and an I
O
O
p
sinterface and split S
y
s
t
e
m
's functionalities into the two. Then you can mock I
O
O
p
s
without mocking F
i
l
e
O
p
s
.
Delegating Calls to a Real Object
When using testing doubles (mocks, fakes, stubs, and etc), sometimes their behaviors will differ from those of the real objects. This difference
could be either intentional (as in simulating an error such that you can test the error handling code) or unintentional. If your mocks have different
behaviors than the real objects by mistake, you could end up with code that passes the tests but fails in production.
You can use the delegating­to­real technique to ensure that your mock has the same behavior as the real object while retaining the ability to
validate calls. This technique is very similar to the delegating­to­fake technique, the difference being that we use a real object instead of a fake.
Here's an example:
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
_
;
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
A
t
L
e
a
s
t
;
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
I
n
v
o
k
e
;
c
l
a
s
s M
o
c
k
F
o
o : p
u
b
l
i
c F
o
o {
p
u
b
l
i
c
:
M
o
c
k
F
o
o
(
) {
/
/ B
y d
e
f
a
u
l
t
, a
l
l c
a
l
l
s a
r
e d
e
l
e
g
a
t
e
d t
o t
h
e r
e
a
l o
b
j
e
c
t
.
O
N
_
C
A
L
L
(
*
t
h
i
s
, D
o
T
h
i
s
(
)
)
.
W
i
l
l
B
y
D
e
f
a
u
l
t
(
I
n
v
o
k
e
(
&
r
e
a
l
_
, &
F
o
o
:
:
D
o
T
h
i
s
)
)
;
O
N
_
C
A
L
L
(
*
t
h
i
s
, D
o
T
h
a
t
(
_
)
)
.
W
i
l
l
B
y
D
e
f
a
u
l
t
(
I
n
v
o
k
e
(
&
r
e
a
l
_
, &
F
o
o
:
:
D
o
T
h
a
t
)
)
;
.
.
.
}
M
O
C
K
_
M
E
T
H
O
D
0
(
D
o
T
h
i
s
, .
.
.
)
;
M
O
C
K
_
M
E
T
H
O
D
1
(
D
o
T
h
a
t
, .
.
.
)
;
.
.
.
p
r
i
v
a
t
e
:
F
o
o r
e
a
l
_
;
}
;
.
.
.
M
o
c
k
F
o
o m
o
c
k
;
E
X
P
E
C
T
_
C
A
L
L
(
m
o
c
k
, D
o
T
h
i
s
(
)
)
.
T
i
m
e
s
(
3
)
;
E
X
P
E
C
T
_
C
A
L
L
(
m
o
c
k
, D
o
T
h
a
t
(
"
H
i
"
)
)
8/23/13 CookBook - googlemock - Google C++ Mocking Framework Cookbook - Google C++ Mocking Framework - Google Project Hosting
https://code.google.com/p/googlemock/wiki/CookBook 9/41
E
X
P
E
C
T
_
C
A
L
L
(
m
o
c
k
, D
o
T
h
a
t
(
"
H
i
"
)
)
.
T
i
m
e
s
(
A
t
L
e
a
s
t
(
1
)
)
;
.
.
. u
s
e m
o
c
k i
n t
e
s
t .
.
.
With this, Google Mock will verify that your code made the right calls (with the right arguments, in the right order, called the right number of
times, etc), and a real object will answer the calls (so the behavior will be the same as in production). This gives you the best of both worlds.
Delegating Calls to a Parent Class
Ideally, you should code to interfaces, whose methods are all pure virtual. In reality, sometimes you do need to mock a virtual method that is not
pure (i.e, it already has an implementation). For example:
c
l
a
s
s F
o
o {
p
u
b
l
i
c
:
v
i
r
t
u
a
l ~
F
o
o
(
)
;
v
i
r
t
u
a
l v
o
i
d P
u
r
e
(
i
n
t n
) = 0
;
v
i
r
t
u
a
l i
n
t C
o
n
c
r
e
t
e
(
c
o
n
s
t c
h
a
r
* s
t
r
) { .
.
. }
}
;
c
l
a
s
s M
o
c
k
F
o
o : p
u
b
l
i
c F
o
o {
p
u
b
l
i
c
:
/
/ M
o
c
k
i
n
g a p
u
r
e m
e
t
h
o
d
.
M
O
C
K
_
M
E
T
H
O
D
1
(
P
u
r
e
, v
o
i
d
(
i
n
t n
)
)
;
/
/ M
o
c
k
i
n
g a c
o
n
c
r
e
t
e m
e
t
h
o
d
. F
o
o
:
:
C
o
n
c
r
e
t
e
(
) i
s s
h
a
d
o
w
e
d
.
M
O
C
K
_
M
E
T
H
O
D
1
(
C
o
n
c
r
e
t
e
, i
n
t
(
c
o
n
s
t c
h
a
r
* s
t
r
)
)
;
}
;
Sometimes you may want to call F
o
o
:
:
C
o
n
c
r
e
t
e
(
)instead of M
o
c
k
F
o
o
:
:
C
o
n
c
r
e
t
e
(
)
. Perhaps you want to do it as part of a stub action, or
perhaps your test doesn't need to mock C
o
n
c
r
e
t
e
(
)at all (but it would be oh­so painful to have to define a new mock class whenever you don't
need to mock one of its methods).
The trick is to leave a back door in your mock class for accessing the real methods in the base class:
c
l
a
s
s M
o
c
k
F
o
o : p
u
b
l
i
c F
o
o {
p
u
b
l
i
c
:
/
/ M
o
c
k
i
n
g a p
u
r
e m
e
t
h
o
d
.
M
O
C
K
_
M
E
T
H
O
D
1
(
P
u
r
e
, v
o
i
d
(
i
n
t n
)
)
;
/
/ M
o
c
k
i
n
g a c
o
n
c
r
e
t
e m
e
t
h
o
d
. F
o
o
:
:
C
o
n
c
r
e
t
e
(
) i
s s
h
a
d
o
w
e
d
.
M
O
C
K
_
M
E
T
H
O
D
1
(
C
o
n
c
r
e
t
e
, i
n
t
(
c
o
n
s
t c
h
a
r
* s
t
r
)
)
;
/
/ U
s
e t
h
i
s t
o c
a
l
l C
o
n
c
r
e
t
e
(
) d
e
f
i
n
e
d i
n F
o
o
.
i
n
t F
o
o
C
o
n
c
r
e
t
e
(
c
o
n
s
t c
h
a
r
* s
t
r
) { r
e
t
u
r
n F
o
o
:
:
C
o
n
c
r
e
t
e
(
s
t
r
)
; }
}
;
Now, you can call F
o
o
:
:
C
o
n
c
r
e
t
e
(
)inside an action by:
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
_
;
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
I
n
v
o
k
e
;
.
.
.
E
X
P
E
C
T
_
C
A
L
L
(
f
o
o
, C
o
n
c
r
e
t
e
(
_
)
)
.
W
i
l
l
O
n
c
e
(
I
n
v
o
k
e
(
&
f
o
o
, &
M
o
c
k
F
o
o
:
:
F
o
o
C
o
n
c
r
e
t
e
)
)
;
or tell the mock object that you don't want to mock C
o
n
c
r
e
t
e
(
)
:
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
I
n
v
o
k
e
;
.
.
.
O
N
_
C
A
L
L
(
f
o
o
, C
o
n
c
r
e
t
e
(
_
)
)
.
W
i
l
l
B
y
D
e
f
a
u
l
t
(
I
n
v
o
k
e
(
&
f
o
o
, &
M
o
c
k
F
o
o
:
:
F
o
o
C
o
n
c
r
e
t
e
)
)
;
(Why don't we just write I
n
v
o
k
e
(
&
f
o
o
, &
F
o
o
:
:
C
o
n
c
r
e
t
e
)
? If you do that, M
o
c
k
F
o
o
:
:
C
o
n
c
r
e
t
e
(
)will be called (and cause an infinite recursion)
since F
o
o
:
:
C
o
n
c
r
e
t
e
(
)is virtual. That's just how C++ works.)
Using Matchers
Matching Argument Values Exactly
You can specify exactly which arguments a mock method is expecting:
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
R
e
t
u
r
n
;
.
.
.
E
X
P
E
C
T
_
C
A
L
L
(
f
o
o
, D
o
T
h
i
s
(
5
)
)
8/23/13 CookBook - googlemock - Google C++ Mocking Framework Cookbook - Google C++ Mocking Framework - Google Project Hosting
https://code.google.com/p/googlemock/wiki/CookBook 10/41
.
W
i
l
l
O
n
c
e
(
R
e
t
u
r
n
(
'
a
'
)
)
;
E
X
P
E
C
T
_
C
A
L
L
(
f
o
o
, D
o
T
h
a
t
(
"
H
e
l
l
o
"
, b
a
r
)
)
;
Using Simple Matchers
You can use matchers to match arguments that have a certain property:
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
G
e
;
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
N
o
t
N
u
l
l
;
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
R
e
t
u
r
n
;
.
.
.
E
X
P
E
C
T
_
C
A
L
L
(
f
o
o
, D
o
T
h
i
s
(
G
e
(
5
)
)
) /
/ T
h
e a
r
g
u
m
e
n
t m
u
s
t b
e >
= 5
.
.
W
i
l
l
O
n
c
e
(
R
e
t
u
r
n
(
'
a
'
)
)
;
E
X
P
E
C
T
_
C
A
L
L
(
f
o
o
, D
o
T
h
a
t
(
"
H
e
l
l
o
"
, N
o
t
N
u
l
l
(
)
)
)
;
/
/ T
h
e s
e
c
o
n
d a
r
g
u
m
e
n
t m
u
s
t n
o
t b
e N
U
L
L
.
A frequently used matcher is _
, which matches anything:
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
_
;
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
N
o
t
N
u
l
l
;
.
.
.
E
X
P
E
C
T
_
C
A
L
L
(
f
o
o
, D
o
T
h
a
t
(
_
, N
o
t
N
u
l
l
(
)
)
)
;
Combining Matchers
You can build complex matchers from existing ones using A
l
l
O
f
(
)
, A
n
y
O
f
(
)
, and N
o
t
(
)
:
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
A
l
l
O
f
;
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
G
t
;
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
H
a
s
S
u
b
s
t
r
;
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
N
e
;
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
N
o
t
;
.
.
.
/
/ T
h
e a
r
g
u
m
e
n
t m
u
s
t b
e > 5 a
n
d !
= 1
0
.
E
X
P
E
C
T
_
C
A
L
L
(
f
o
o
, D
o
T
h
i
s
(
A
l
l
O
f
(
G
t
(
5
)
,
N
e
(
1
0
)
)
)
)
;
/
/ T
h
e f
i
r
s
t a
r
g
u
m
e
n
t m
u
s
t n
o
t c
o
n
t
a
i
n s
u
b
-
s
t
r
i
n
g "
b
l
a
h
"
.
E
X
P
E
C
T
_
C
A
L
L
(
f
o
o
, D
o
T
h
a
t
(
N
o
t
(
H
a
s
S
u
b
s
t
r
(
"
b
l
a
h
"
)
)
,
N
U
L
L
)
)
;
Casting Matchers
Google Mock matchers are statically typed, meaning that the compiler can catch your mistake if you use a matcher of the wrong type (for
example, if you use E
q
(
5
)to match a s
t
r
i
n
gargument). Good for you!
Sometimes, however, you know what you're doing and want the compiler to give you some slack. One example is that you have a matcher for
l
o
n
gand the argument you want to match is i
n
t
. While the two types aren't exactly the same, there is nothing really wrong with using a
M
a
t
c
h
e
r
<
l
o
n
g
>to match an i
n
t­ after all, we can first convert the i
n
targument to a l
o
n
gbefore giving it to the matcher.
To support this need, Google Mock gives you the S
a
f
e
M
a
t
c
h
e
r
C
a
s
t
<
T
>
(
m
)function. It casts a matcher mto type M
a
t
c
h
e
r
<
T
>
. To ensure
safety, Google Mock checks that (let Ube the type maccepts):
1. Type Tcan be implicitly cast to type U
;
2. When both Tand Uare built­in arithmetic types (b
o
o
l
, integers, and floating­point numbers), the conversion from Tto Uis not lossy (in other
words, any value representable by Tcan also be represented by U
); and
3. When Uis a reference, Tmust also be a reference (as the underlying matcher may be interested in the address of the Uvalue).
The code won't compile if any of these conditions isn't met.
Here's one example:
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
S
a
f
e
M
a
t
c
h
e
r
C
a
s
t
;
/
/ A b
a
s
e c
l
a
s
s a
n
d a c
h
i
l
d c
l
a
s
s
.
c
l
a
s
s B
a
s
e { .
.
. }
;
c
l
a
s
s D
e
r
i
v
e
d : p
u
b
l
i
c B
a
s
e { .
.
. }
;
c
l
a
s
s M
o
c
k
F
o
o : p
u
b
l
i
c F
o
o {
p
u
b
l
i
c
:
M
O
C
K
_
M
E
T
H
O
D
1
(
D
o
T
h
i
s
, v
o
i
d
(
D
e
r
i
v
e
d
* d
e
r
i
v
e
d
)
)
;
8/23/13 CookBook - googlemock - Google C++ Mocking Framework Cookbook - Google C++ Mocking Framework - Google Project Hosting
https://code.google.com/p/googlemock/wiki/CookBook 11/41
}
;
.
.
.
M
o
c
k
F
o
o f
o
o
;
/
/ m i
s a M
a
t
c
h
e
r
<
B
a
s
e
*
> w
e g
o
t f
r
o
m s
o
m
e
w
h
e
r
e
.
E
X
P
E
C
T
_
C
A
L
L
(
f
o
o
, D
o
T
h
i
s
(
S
a
f
e
M
a
t
c
h
e
r
C
a
s
t
<
D
e
r
i
v
e
d
*
>
(
m
)
)
)
;
If you find S
a
f
e
M
a
t
c
h
e
r
C
a
s
t
<
T
>
(
m
)too limiting, you can use a similar function M
a
t
c
h
e
r
C
a
s
t
<
T
>
(
m
)
. The difference is that M
a
t
c
h
e
r
C
a
s
tworks
as long as you can s
t
a
t
i
c
_
c
a
s
ttype Tto type U
.
M
a
t
c
h
e
r
C
a
s
tessentially lets you bypass C++'s type system (s
t
a
t
i
c
_
c
a
s
tisn't always safe as it could throw away information, for example),
so be careful not to misuse/abuse it.
Selecting Between Overloaded Functions
If you expect an overloaded function to be called, the compiler may need some help on which overloaded version it is.
To disambiguate functions overloaded on the const­ness of this object, use the C
o
n
s
t
(
)argument wrapper.
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
R
e
t
u
r
n
R
e
f
;
c
l
a
s
s M
o
c
k
F
o
o : p
u
b
l
i
c F
o
o {
.
.
.
M
O
C
K
_
M
E
T
H
O
D
0
(
G
e
t
B
a
r
, B
a
r
&
(
)
)
;
M
O
C
K
_
C
O
N
S
T
_
M
E
T
H
O
D
0
(
G
e
t
B
a
r
, c
o
n
s
t B
a
r
&
(
)
)
;
}
;
.
.
.
M
o
c
k
F
o
o f
o
o
;
B
a
r b
a
r
1
, b
a
r
2
;
E
X
P
E
C
T
_
C
A
L
L
(
f
o
o
, G
e
t
B
a
r
(
)
) /
/ T
h
e n
o
n
-
c
o
n
s
t G
e
t
B
a
r
(
)
.
.
W
i
l
l
O
n
c
e
(
R
e
t
u
r
n
R
e
f
(
b
a
r
1
)
)
;
E
X
P
E
C
T
_
C
A
L
L
(
C
o
n
s
t
(
f
o
o
)
, G
e
t
B
a
r
(
)
) /
/ T
h
e c
o
n
s
t G
e
t
B
a
r
(
)
.
.
W
i
l
l
O
n
c
e
(
R
e
t
u
r
n
R
e
f
(
b
a
r
2
)
)
;
(C
o
n
s
t
(
)is defined by Google Mock and returns a c
o
n
s
treference to its argument.)
To disambiguate overloaded functions with the same number of arguments but different argument types, you may need to specify the exact type
of a matcher, either by wrapping your matcher in M
a
t
c
h
e
r
<
t
y
p
e
>
(
)
, or using a matcher whose type is fixed (T
y
p
e
d
E
q
<
t
y
p
e
>
, A
n
<
t
y
p
e
>
(
)
, etc):
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
A
n
;
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
L
t
;
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
M
a
t
c
h
e
r
;
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
T
y
p
e
d
E
q
;
c
l
a
s
s M
o
c
k
P
r
i
n
t
e
r : p
u
b
l
i
c P
r
i
n
t
e
r {
p
u
b
l
i
c
:
M
O
C
K
_
M
E
T
H
O
D
1
(
P
r
i
n
t
, v
o
i
d
(
i
n
t n
)
)
;
M
O
C
K
_
M
E
T
H
O
D
1
(
P
r
i
n
t
, v
o
i
d
(
c
h
a
r c
)
)
;
}
;
T
E
S
T
(
P
r
i
n
t
e
r
T
e
s
t
, P
r
i
n
t
) {
M
o
c
k
P
r
i
n
t
e
r p
r
i
n
t
e
r
;
E
X
P
E
C
T
_
C
A
L
L
(
p
r
i
n
t
e
r
, P
r
i
n
t
(
A
n
<
i
n
t
>
(
)
)
)
; /
/ v
o
i
d P
r
i
n
t
(
i
n
t
)
;
E
X
P
E
C
T
_
C
A
L
L
(
p
r
i
n
t
e
r
, P
r
i
n
t
(
M
a
t
c
h
e
r
<
i
n
t
>
(
L
t
(
5
)
)
)
)
; /
/ v
o
i
d P
r
i
n
t
(
i
n
t
)
;
E
X
P
E
C
T
_
C
A
L
L
(
p
r
i
n
t
e
r
, P
r
i
n
t
(
T
y
p
e
d
E
q
<
c
h
a
r
>
(
'
a
'
)
)
)
; /
/ v
o
i
d P
r
i
n
t
(
c
h
a
r
)
;
p
r
i
n
t
e
r
.
P
r
i
n
t
(
3
)
;
p
r
i
n
t
e
r
.
P
r
i
n
t
(
6
)
;
p
r
i
n
t
e
r
.
P
r
i
n
t
(
'
a
'
)
;
}
Performing Different Actions Based on the Arguments
When a mock method is called, the last matching expectation that's still active will be selected (think "newer overrides older"). So, you can
make a method do different things depending on its argument values like this:
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
_
;
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
L
t
;
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
R
e
t
u
r
n
;
.
.
.
/
/ T
h
e d
e
f
a
u
l
t c
a
s
e
.
8/23/13 CookBook - googlemock - Google C++ Mocking Framework Cookbook - Google C++ Mocking Framework - Google Project Hosting
https://code.google.com/p/googlemock/wiki/CookBook 12/41
/
/ T
h
e d
e
f
a
u
l
t c
a
s
e
.
E
X
P
E
C
T
_
C
A
L
L
(
f
o
o
, D
o
T
h
i
s
(
_
)
)
.
W
i
l
l
R
e
p
e
a
t
e
d
l
y
(
R
e
t
u
r
n
(
'
b
'
)
)
;
/
/ T
h
e m
o
r
e s
p
e
c
i
f
i
c c
a
s
e
.
E
X
P
E
C
T
_
C
A
L
L
(
f
o
o
, D
o
T
h
i
s
(
L
t
(
5
)
)
)
.
W
i
l
l
R
e
p
e
a
t
e
d
l
y
(
R
e
t
u
r
n
(
'
a
'
)
)
;
Now, if f
o
o
.
D
o
T
h
i
s
(
)is called with a value less than 5, '
a
'will be returned; otherwise '
b
'will be returned.
Matching Multiple Arguments as a Whole
Sometimes it's not enough to match the arguments individually. For example, we may want to say that the first argument must be less than the
second argument. The W
i
t
h
(
)clause allows us to match all arguments of a mock function as a whole. For example,
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
_
;
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
L
t
;
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
N
e
;
.
.
.
E
X
P
E
C
T
_
C
A
L
L
(
f
o
o
, I
n
R
a
n
g
e
(
N
e
(
0
)
, _
)
)
.
W
i
t
h
(
L
t
(
)
)
;
says that the first argument of I
n
R
a
n
g
e
(
)must not be 0, and must be less than the second argument.
The expression inside W
i
t
h
(
)must be a matcher of type M
a
t
c
h
e
r
<
t
r
1
:
:
t
u
p
l
e
<
A
1
, .
.
.
, A
n
> >
, where A
1
, ..., A
nare the types of the function
arguments.
You can also write A
l
l
A
r
g
s
(
m
)instead of minside .
W
i
t
h
(
)
. The two forms are equivalent, but .
W
i
t
h
(
A
l
l
A
r
g
s
(
L
t
(
)
)
)is more readable than
.
W
i
t
h
(
L
t
(
)
)
.
You can use A
r
g
s
<
k
1
, .
.
.
, k
n
>
(
m
)to match the nselected arguments (as a tuple) against m
. For example,
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
_
;
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
A
l
l
O
f
;
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
A
r
g
s
;
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
L
t
;
.
.
.
E
X
P
E
C
T
_
C
A
L
L
(
f
o
o
, B
l
a
h
(
_
, _
, _
)
)
.
W
i
t
h
(
A
l
l
O
f
(
A
r
g
s
<
0
, 1
>
(
L
t
(
)
)
, A
r
g
s
<
1
, 2
>
(
L
t
(
)
)
)
)
;
says that B
l
a
h
(
)will be called with arguments x
, y
, and zwhere x < y < z
.
As a convenience and example, Google Mock provides some matchers for 2­tuples, including the L
t
(
)matcher above. See the CheatSheet for
the complete list.
Note that if you want to pass the arguments to a predicate of your own (e.g. .
W
i
t
h
(
A
r
g
s
<
0
, 1
>
(
T
r
u
l
y
(
&
M
y
P
r
e
d
i
c
a
t
e
)
)
)
), that predicate
MUST be written to take a t
r
1
:
:
t
u
p
l
eas its argument; Google Mock will pass the nselected arguments as one single tuple to the predicate.
Using Matchers as Predicates
Have you noticed that a matcher is just a fancy predicate that also knows how to describe itself? Many existing algorithms take predicates as
arguments (e.g. those defined in STL's <
a
l
g
o
r
i
t
h
m
>header), and it would be a shame if Google Mock matchers are not allowed to participate.
Luckily, you can use a matcher where a unary predicate functor is expected by wrapping it inside the M
a
t
c
h
e
s
(
)function. For example,
#
i
n
c
l
u
d
e <
a
l
g
o
r
i
t
h
m
>
#
i
n
c
l
u
d
e <
v
e
c
t
o
r
>
s
t
d
:
:
v
e
c
t
o
r
<
i
n
t
> v
;
.
.
.
/
/ H
o
w m
a
n
y e
l
e
m
e
n
t
s i
n v a
r
e >
= 1
0
?
c
o
n
s
t i
n
t c
o
u
n
t = c
o
u
n
t
_
i
f
(
v
.
b
e
g
i
n
(
)
, v
.
e
n
d
(
)
, M
a
t
c
h
e
s
(
G
e
(
1
0
)
)
)
;
Since you can build complex matchers from simpler ones easily using Google Mock, this gives you a way to conveniently construct composite
predicates (doing the same using STL's <
f
u
n
c
t
i
o
n
a
l
>header is just painful). For example, here's a predicate that's satisfied by any number that
is >= 0, <= 100, and != 50:
M
a
t
c
h
e
s
(
A
l
l
O
f
(
G
e
(
0
)
, L
e
(
1
0
0
)
, N
e
(
5
0
)
)
)
Using Matchers in Google Test Assertions
Since matchers are basically predicates that also know how to describe themselves, there is a way to take advantage of them in Google Test
assertions. It's called A
S
S
E
R
T
_
T
H
A
Tand E
X
P
E
C
T
_
T
H
A
T
:
8/23/13 CookBook - googlemock - Google C++ Mocking Framework Cookbook - Google C++ Mocking Framework - Google Project Hosting
https://code.google.com/p/googlemock/wiki/CookBook 13/41
A
S
S
E
R
T
_
T
H
A
T
(
v
a
l
u
e
, m
a
t
c
h
e
r
)
; /
/ A
s
s
e
r
t
s t
h
a
t v
a
l
u
e m
a
t
c
h
e
s m
a
t
c
h
e
r
.
E
X
P
E
C
T
_
T
H
A
T
(
v
a
l
u
e
, m
a
t
c
h
e
r
)
; /
/ T
h
e n
o
n
-
f
a
t
a
l v
e
r
s
i
o
n
.
For example, in a Google Test test you can write:
#
i
n
c
l
u
d
e "
g
m
o
c
k
/
g
m
o
c
k
.
h
"
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
A
l
l
O
f
;
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
G
e
;
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
L
e
;
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
M
a
t
c
h
e
s
R
e
g
e
x
;
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
S
t
a
r
t
s
W
i
t
h
;
.
.
.
E
X
P
E
C
T
_
T
H
A
T
(
F
o
o
(
)
, S
t
a
r
t
s
W
i
t
h
(
"
H
e
l
l
o
"
)
)
;
E
X
P
E
C
T
_
T
H
A
T
(
B
a
r
(
)
, M
a
t
c
h
e
s
R
e
g
e
x
(
"
L
i
n
e 

d
+
"
)
)
;
A
S
S
E
R
T
_
T
H
A
T
(
B
a
z
(
)
, A
l
l
O
f
(
G
e
(
5
)
, L
e
(
1
0
)
)
)
;
which (as you can probably guess) executes F
o
o
(
)
, B
a
r
(
)
, and B
a
z
(
)
, and verifies that:
F
o
o
(
)returns a string that starts with "
H
e
l
l
o
"
.
B
a
r
(
)returns a string that matches regular expression "
L
i
n
e 

d
+
"
.
B
a
z
(
)returns a number in the range [5, 10].
The nice thing about these macros is that they read like English. They generate informative messages too. For example, if the first
E
X
P
E
C
T
_
T
H
A
T
(
)above fails, the message will be something like:
V
a
l
u
e o
f
: F
o
o
(
)
A
c
t
u
a
l
: "
H
i
, w
o
r
l
d
!
"
E
x
p
e
c
t
e
d
: s
t
a
r
t
s w
i
t
h "
H
e
l
l
o
"
Credit: The idea of (
A
S
S
E
R
T
|
E
X
P
E
C
T
)
_
T
H
A
Twas stolen from the Hamcrest project, which adds a
s
s
e
r
t
T
h
a
t
(
)to JUnit.
Using Predicates as Matchers
Google Mock provides a built­in set of matchers. In case you find them lacking, you can use an arbitray unary predicate function or functor as a
matcher ­ as long as the predicate accepts a value of the type you want. You do this by wrapping the predicate inside the T
r
u
l
y
(
)function, for
example:
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
T
r
u
l
y
;
i
n
t I
s
E
v
e
n
(
i
n
t n
) { r
e
t
u
r
n (
n % 2
) =
= 0 ? 1 : 0
; }
.
.
.
/
/ B
a
r
(
) m
u
s
t b
e c
a
l
l
e
d w
i
t
h a
n e
v
e
n n
u
m
b
e
r
.
E
X
P
E
C
T
_
C
A
L
L
(
f
o
o
, B
a
r
(
T
r
u
l
y
(
I
s
E
v
e
n
)
)
)
;
Note that the predicate function / functor doesn't have to return b
o
o
l
. It works as long as the return value can be used as the condition in
statement i
f (
c
o
n
d
i
t
i
o
n
) .
.
.
.
Matching Arguments that Are Not Copyable
When you do an E
X
P
E
C
T
_
C
A
L
L
(
m
o
c
k
_
o
b
j
, F
o
o
(
b
a
r
)
)
, Google Mock saves away a copy of b
a
r
. When F
o
o
(
)is called later, Google Mock
compares the argument to F
o
o
(
)with the saved copy of b
a
r
. This way, you don't need to worry about b
a
rbeing modified or destroyed after the
E
X
P
E
C
T
_
C
A
L
L
(
)is executed. The same is true when you use matchers like E
q
(
b
a
r
)
, L
e
(
b
a
r
)
, and so on.
But what if b
a
rcannot be copied (i.e. has no copy constructor)? You could define your own matcher function and use it with T
r
u
l
y
(
)
, as the
previous couple of recipes have shown. Or, you may be able to get away from it if you can guarantee that b
a
rwon't be changed after the
E
X
P
E
C
T
_
C
A
L
L
(
)is executed. Just tell Google Mock that it should save a reference to b
a
r
, instead of a copy of it. Here's how:
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
E
q
;
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
B
y
R
e
f
;
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
L
t
;
.
.
.
/
/ E
x
p
e
c
t
s t
h
a
t F
o
o
(
)
'
s a
r
g
u
m
e
n
t =
= b
a
r
.
E
X
P
E
C
T
_
C
A
L
L
(
m
o
c
k
_
o
b
j
, F
o
o
(
E
q
(
B
y
R
e
f
(
b
a
r
)
)
)
)
;
/
/ E
x
p
e
c
t
s t
h
a
t F
o
o
(
)
'
s a
r
g
u
m
e
n
t < b
a
r
.
E
X
P
E
C
T
_
C
A
L
L
(
m
o
c
k
_
o
b
j
, F
o
o
(
L
t
(
B
y
R
e
f
(
b
a
r
)
)
)
)
;
8/23/13 CookBook - googlemock - Google C++ Mocking Framework Cookbook - Google C++ Mocking Framework - Google Project Hosting
https://code.google.com/p/googlemock/wiki/CookBook 14/41
Remember: if you do this, don't change b
a
rafter the E
X
P
E
C
T
_
C
A
L
L
(
)
, or the result is undefined.
Validating a Member of an Object
Often a mock function takes a reference to object as an argument. When matching the argument, you may not want to compare the entire object
against a fixed object, as that may be over­specification. Instead, you may need to validate a certain member variable or the result of a certain
getter method of the object. You can do this with F
i
e
l
d
(
)and P
r
o
p
e
r
t
y
(
)
. More specifically,
F
i
e
l
d
(
&
F
o
o
:
:
b
a
r
, m
)
is a matcher that matches a F
o
oobject whose b
a
rmember variable satisfies matcher m
.
P
r
o
p
e
r
t
y
(
&
F
o
o
:
:
b
a
z
, m
)
is a matcher that matches a F
o
oobject whose b
a
z
(
)method returns a value that satisfies matcher m
.
For example:
F
i
e
l
d
(
&
F
o
o
:
:
n
u
m
b
e
r
, G
e
(
3
)
) Matches xwhere x
.
n
u
m
b
e
r >
= 3
.
P
r
o
p
e
r
t
y
(
&
F
o
o
:
:
n
a
m
e
, S
t
a
r
t
s
W
i
t
h
(
"
J
o
h
n "
)
) Matches xwhere x
.
n
a
m
e
(
)starts with "
J
o
h
n "
.
Note that in P
r
o
p
e
r
t
y
(
&
F
o
o
:
:
b
a
z
, .
.
.
)
, method b
a
z
(
)must take no argument and be declared as c
o
n
s
t
.
BTW, F
i
e
l
d
(
)and P
r
o
p
e
r
t
y
(
)can also match plain pointers to objects. For instance,
F
i
e
l
d
(
&
F
o
o
:
:
n
u
m
b
e
r
, G
e
(
3
)
)
matches a plain pointer pwhere p
-
>
n
u
m
b
e
r >
= 3
. If pis N
U
L
L
, the match will always fail regardless of the inner matcher.
What if you want to validate more than one members at the same time? Remember that there is A
l
l
O
f
(
)
.
Validating the Value Pointed to by a Pointer Argument
C++ functions often take pointers as arguments. You can use matchers like I
s
N
u
l
l
(
)
, N
o
t
N
u
l
l
(
)
, and other comparison matchers to match a
pointer, but what if you want to make sure the value pointed to by the pointer, instead of the pointer itself, has a certain property? Well, you can
use the P
o
i
n
t
e
e
(
m
)matcher.
P
o
i
n
t
e
e
(
m
)matches a pointer iff mmatches the value the pointer points to. For example:
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
G
e
;
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
P
o
i
n
t
e
e
;
.
.
.
E
X
P
E
C
T
_
C
A
L
L
(
f
o
o
, B
a
r
(
P
o
i
n
t
e
e
(
G
e
(
3
)
)
)
)
;
expects f
o
o
.
B
a
r
(
)to be called with a pointer that points to a value greater than or equal to 3.
One nice thing about P
o
i
n
t
e
e
(
)is that it treats a N
U
L
Lpointer as a match failure, so you can write P
o
i
n
t
e
e
(
m
)instead of
A
l
l
O
f
(
N
o
t
N
u
l
l
(
)
, P
o
i
n
t
e
e
(
m
)
)
without worrying that a N
U
L
Lpointer will crash your test.
Also, did we tell you that P
o
i
n
t
e
e
(
)works with both raw pointers and smart pointers (l
i
n
k
e
d
_
p
t
r
, s
h
a
r
e
d
_
p
t
r
, s
c
o
p
e
d
_
p
t
r
, and etc)?
What if you have a pointer to pointer? You guessed it ­ you can use nested P
o
i
n
t
e
e
(
)to probe deeper inside the value. For example,
P
o
i
n
t
e
e
(
P
o
i
n
t
e
e
(
L
t
(
3
)
)
)matches a pointer that points to a pointer that points to a number less than 3 (what a mouthful...).
Testing a Certain Property of an Object
Sometimes you want to specify that an object argument has a certain property, but there is no existing matcher that does this. If you want good
error messages, you should define a matcher. If you want to do it quick and dirty, you could get away with writing an ordinary function.
Let's say you have a mock function that takes an object of type F
o
o
, which has an i
n
t b
a
r
(
)method and an i
n
t b
a
z
(
)method, and you want
to constrain that the argument's b
a
r
(
)value plus its b
a
z
(
)value is a given number. Here's how you can define a matcher to do it:
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
M
a
t
c
h
e
r
I
n
t
e
r
f
a
c
e
;
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
M
a
t
c
h
R
e
s
u
l
t
L
i
s
t
e
n
e
r
;
c
l
a
s
s B
a
r
P
l
u
s
B
a
z
E
q
M
a
t
c
h
e
r : p
u
b
l
i
c M
a
t
c
h
e
r
I
n
t
e
r
f
a
c
e
<
c
o
n
s
t F
o
o
&
> {
p
u
b
l
i
c
:
8/23/13 CookBook - googlemock - Google C++ Mocking Framework Cookbook - Google C++ Mocking Framework - Google Project Hosting
https://code.google.com/p/googlemock/wiki/CookBook 15/41
p
u
b
l
i
c
:
e
x
p
l
i
c
i
t B
a
r
P
l
u
s
B
a
z
E
q
M
a
t
c
h
e
r
(
i
n
t e
x
p
e
c
t
e
d
_
s
u
m
)
: e
x
p
e
c
t
e
d
_
s
u
m
_
(
e
x
p
e
c
t
e
d
_
s
u
m
) {
}
v
i
r
t
u
a
l b
o
o
l M
a
t
c
h
A
n
d
E
x
p
l
a
i
n
(
c
o
n
s
t F
o
o
& f
o
o
,
M
a
t
c
h
R
e
s
u
l
t
L
i
s
t
e
n
e
r
* l
i
s
t
e
n
e
r
) c
o
n
s
t {
r
e
t
u
r
n (
f
o
o
.
b
a
r
(
) + f
o
o
.
b
a
z
(
)
) =
= e
x
p
e
c
t
e
d
_
s
u
m
_
;
}
v
i
r
t
u
a
l v
o
i
d D
e
s
c
r
i
b
e
T
o
(
:
:
s
t
d
:
:
o
s
t
r
e
a
m
* o
s
) c
o
n
s
t {
*
o
s <
< "
b
a
r
(
) + b
a
z
(
) e
q
u
a
l
s " <
< e
x
p
e
c
t
e
d
_
s
u
m
_
;
}
v
i
r
t
u
a
l v
o
i
d D
e
s
c
r
i
b
e
N
e
g
a
t
i
o
n
T
o
(
:
:
s
t
d
:
:
o
s
t
r
e
a
m
* o
s
) c
o
n
s
t {
*
o
s <
< "
b
a
r
(
) + b
a
z
(
) d
o
e
s n
o
t e
q
u
a
l " <
< e
x
p
e
c
t
e
d
_
s
u
m
_
;
}
p
r
i
v
a
t
e
:
c
o
n
s
t i
n
t e
x
p
e
c
t
e
d
_
s
u
m
_
;
}
;
i
n
l
i
n
e M
a
t
c
h
e
r
<
c
o
n
s
t F
o
o
&
> B
a
r
P
l
u
s
B
a
z
E
q
(
i
n
t e
x
p
e
c
t
e
d
_
s
u
m
) {
r
e
t
u
r
n M
a
k
e
M
a
t
c
h
e
r
(
n
e
w B
a
r
P
l
u
s
B
a
z
E
q
M
a
t
c
h
e
r
(
e
x
p
e
c
t
e
d
_
s
u
m
)
)
;
}
.
.
.
E
X
P
E
C
T
_
C
A
L
L
(
.
.
.
, D
o
T
h
i
s
(
B
a
r
P
l
u
s
B
a
z
E
q
(
5
)
)
)
.
.
.
;
Matching Containers
Sometimes an STL container (e.g. list, vector, map, ...) is passed to a mock function and you may want to validate it. Since most STL
containers support the =
=operator, you can write E
q
(
e
x
p
e
c
t
e
d
_
c
o
n
t
a
i
n
e
r
)or simply e
x
p
e
c
t
e
d
_
c
o
n
t
a
i
n
e
rto match a container exactly.
Sometimes, though, you may want to be more flexible (for example, the first element must be an exact match, but the second element can be
any positive number, and so on). Also, containers used in tests often have a small number of elements, and having to define the expected
container out­of­line is a bit of a hassle.
You can use the E
l
e
m
e
n
t
s
A
r
e
(
)or U
n
o
r
d
e
r
e
d
E
l
e
m
e
n
t
s
A
r
e
(
)matcher in such cases:
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
_
;
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
E
l
e
m
e
n
t
s
A
r
e
;
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
G
t
;
.
.
.
M
O
C
K
_
M
E
T
H
O
D
1
(
F
o
o
, v
o
i
d
(
c
o
n
s
t v
e
c
t
o
r
<
i
n
t
>
& n
u
m
b
e
r
s
)
)
;
.
.
.
E
X
P
E
C
T
_
C
A
L
L
(
m
o
c
k
, F
o
o
(
E
l
e
m
e
n
t
s
A
r
e
(
1
, G
t
(
0
)
, _
, 5
)
)
)
;
The above matcher says that the container must have 4 elements, which must be 1, greater than 0, anything, and 5 respectively.
If you instead write:
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
_
;
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
G
t
;
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
U
n
o
r
d
e
r
e
d
E
l
e
m
e
n
t
s
A
r
e
;
.
.
.
M
O
C
K
_
M
E
T
H
O
D
1
(
F
o
o
, v
o
i
d
(
c
o
n
s
t v
e
c
t
o
r
<
i
n
t
>
& n
u
m
b
e
r
s
)
)
;
.
.
.
E
X
P
E
C
T
_
C
A
L
L
(
m
o
c
k
, F
o
o
(
U
n
o
r
d
e
r
e
d
E
l
e
m
e
n
t
s
A
r
e
(
1
, G
t
(
0
)
, _
, 5
)
)
)
;
It means that the container must have 4 elements, which under some permutation must be 1, greater than 0, anything, and 5 respectively.
E
l
e
m
e
n
t
s
A
r
e
(
)and U
n
o
r
d
e
r
e
d
E
l
e
m
e
n
t
s
A
r
e
(
)are overloaded to take 0 to 10 arguments. If more are needed, you can place them in a C­style
array and use E
l
e
m
e
n
t
s
A
r
e
A
r
r
a
y
(
)or U
n
o
r
d
e
r
e
d
E
l
e
m
e
n
t
s
A
r
e
A
r
r
a
y
(
)instead:
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
E
l
e
m
e
n
t
s
A
r
e
A
r
r
a
y
;
.
.
.
/
/ E
l
e
m
e
n
t
s
A
r
e
A
r
r
a
y a
c
c
e
p
t
s a
n a
r
r
a
y o
f e
l
e
m
e
n
t v
a
l
u
e
s
.
c
o
n
s
t i
n
t e
x
p
e
c
t
e
d
_
v
e
c
t
o
r
1
[
] = { 1
, 5
, 2
, 4
, .
.
. }
;
8/23/13 CookBook - googlemock - Google C++ Mocking Framework Cookbook - Google C++ Mocking Framework - Google Project Hosting
https://code.google.com/p/googlemock/wiki/CookBook 16/41
c
o
n
s
t i
n
t e
x
p
e
c
t
e
d
_
v
e
c
t
o
r
1
[
] = { 1
, 5
, 2
, 4
, .
.
. }
;
E
X
P
E
C
T
_
C
A
L
L
(
m
o
c
k
, F
o
o
(
E
l
e
m
e
n
t
s
A
r
e
A
r
r
a
y
(
e
x
p
e
c
t
e
d
_
v
e
c
t
o
r
1
)
)
)
;
/
/ O
r
, a
n a
r
r
a
y o
f e
l
e
m
e
n
t m
a
t
c
h
e
r
s
.
M
a
t
c
h
e
r
<
i
n
t
> e
x
p
e
c
t
e
d
_
v
e
c
t
o
r
2 = { 1
, G
t
(
2
)
, _
, 3
, .
.
. }
;
E
X
P
E
C
T
_
C
A
L
L
(
m
o
c
k
, F
o
o
(
E
l
e
m
e
n
t
s
A
r
e
A
r
r
a
y
(
e
x
p
e
c
t
e
d
_
v
e
c
t
o
r
2
)
)
)
;
In case the array needs to be dynamically created (and therefore the array size cannot be inferred by the compiler), you can give
E
l
e
m
e
n
t
s
A
r
e
A
r
r
a
y
(
)an additional argument to specify the array size:
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
E
l
e
m
e
n
t
s
A
r
e
A
r
r
a
y
;
.
.
.
i
n
t
* c
o
n
s
t e
x
p
e
c
t
e
d
_
v
e
c
t
o
r
3 = n
e
w i
n
t
[
c
o
u
n
t
]
;
.
.
. f
i
l
l e
x
p
e
c
t
e
d
_
v
e
c
t
o
r
3 w
i
t
h v
a
l
u
e
s .
.
.
E
X
P
E
C
T
_
C
A
L
L
(
m
o
c
k
, F
o
o
(
E
l
e
m
e
n
t
s
A
r
e
A
r
r
a
y
(
e
x
p
e
c
t
e
d
_
v
e
c
t
o
r
3
, c
o
u
n
t
)
)
)
;
Tips:
E
l
e
m
e
n
t
A
r
e
*
(
)works with any container that implements the STL iterator concept (i.e. it has a c
o
n
s
t
_
i
t
e
r
a
t
o
rtype and supports
b
e
g
i
n
(
)
/
e
n
d
(
)
) and supports s
i
z
e
(
)
, not just the ones defined in STL. It will even work with container types yet to be written ­ as long as
they follows the above pattern.
You can use nested E
l
e
m
e
n
t
A
r
e
*
(
)to match nested (multi­dimensional) containers.
If the container is passed by pointer instead of by reference, just write P
o
i
n
t
e
e
(
E
l
e
m
e
n
t
s
A
r
e
*
(
.
.
.
)
)
.
The order of elements matters for E
l
e
m
e
n
t
s
A
r
e
*
(
)
. Therefore don't use it with containers whose element order is undefined (e.g. h
a
s
h
_
m
a
p
).
Sharing Matchers
Under the hood, a Google Mock matcher object consists of a pointer to a ref­counted implementation object. Copying matchers is allowed and
very efficient, as only the pointer is copied. When the last matcher that references the implementation object dies, the implementation object will
be deleted.
Therefore, if you have some complex matcher that you want to use again and again, there is no need to build it everytime. Just assign it to a
matcher variable and use that variable repeatedly! For example,
M
a
t
c
h
e
r
<
i
n
t
> i
n
_
r
a
n
g
e = A
l
l
O
f
(
G
t
(
5
)
, L
e
(
1
0
)
)
;
.
.
. u
s
e i
n
_
r
a
n
g
e a
s a m
a
t
c
h
e
r i
n m
u
l
t
i
p
l
e E
X
P
E
C
T
_
C
A
L
L
s .
.
.
Setting Expectations
Knowing When to Expect
O
N
_
C
A
L
Lis likely the single most under­utilized construct in Google Mock.
There are basically two constructs for defining the behavior of a mock object: O
N
_
C
A
L
Land E
X
P
E
C
T
_
C
A
L
L
. The difference? O
N
_
C
A
L
Ldefines what
happens when a mock method is called, but doesn't imply any expectation on the method being called. E
X
P
E
C
T
_
C
A
L
Lnot only defines the
behavior, but also sets an expectation that the method will be called with the given arguments, for the given number of times (and in the given
order when you specify the order too).
Since E
X
P
E
C
T
_
C
A
L
Ldoes more, isn't it better than O
N
_
C
A
L
L
? Not really. Every E
X
P
E
C
T
_
C
A
L
Ladds a constraint on the behavior of the code under
test. Having more constraints than necessary is baaad ­ even worse than not having enough constraints.
This may be counter­intuitive. How could tests that verify more be worse than tests that verify less? Isn't verification the whole point of tests?
The answer, lies in what a test should verify. A good test verifies the contract of the code. If a test over­specifies, it doesn't leave enough
freedom to the implementation. As a result, changing the implementation without breaking the contract (e.g. refactoring and optimization), which
should be perfectly fine to do, can break such tests. Then you have to spend time fixing them, only to see them broken again the next time the
implementation is changed.
Keep in mind that one doesn't have to verify more than one property in one test. In fact, it's a good style to verify only one thing in one test.
If you do that, a bug will likely break only one or two tests instead of dozens (which case would you rather debug?). If you are also in the habit of
giving tests descriptive names that tell what they verify, you can often easily guess what's wrong just from the test log itself.
So use O
N
_
C
A
L
Lby default, and only use E
X
P
E
C
T
_
C
A
L
Lwhen you actually intend to verify that the call is made. For example, you may have a
bunch of O
N
_
C
A
L
L
s in your test fixture to set the common mock behavior shared by all tests in the same group, and write (scarcely) different
E
X
P
E
C
T
_
C
A
L
L
s in different T
E
S
T
_
F
s to verify different aspects of the code's behavior. Compared with the style where each T
E
S
Thas many
E
X
P
E
C
T
_
C
A
L
L
s, this leads to tests that are more resilient to implementational changes (and thus less likely to require maintenance) and makes
the intent of the tests more obvious (so they are easier to maintain when you do need to maintain them).
Ignoring Uninteresting Calls
If you are not interested in how a mock method is called, just don't say anything about it. In this case, if the method is ever called, Google Mock
will perform its default action to allow the test program to continue. If you are not happy with the default action taken by Google Mock, you can
override it using D
e
f
a
u
l
t
V
a
l
u
e
<
T
>
:
:
S
e
t
(
)(described later in this document) or O
N
_
C
A
L
L
(
)
.
8/23/13 CookBook - googlemock - Google C++ Mocking Framework Cookbook - Google C++ Mocking Framework - Google Project Hosting
https://code.google.com/p/googlemock/wiki/CookBook 17/41
override it using D
e
f
a
u
l
t
V
a
l
u
e
<
T
>
:
:
S
e
t
(
)(described later in this document) or O
N
_
C
A
L
L
(
)
.
Please note that once you expressed interest in a particular mock method (via E
X
P
E
C
T
_
C
A
L
L
(
)
), all invocations to it must match some
expectation. If this function is called but the arguments don't match any E
X
P
E
C
T
_
C
A
L
L
(
)statement, it will be an error.
Disallowing Unexpected Calls
If a mock method shouldn't be called at all, explicitly say so:
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
_
;
.
.
.
E
X
P
E
C
T
_
C
A
L
L
(
f
o
o
, B
a
r
(
_
)
)
.
T
i
m
e
s
(
0
)
;
If some calls to the method are allowed, but the rest are not, just list all the expected calls:
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
A
n
y
N
u
m
b
e
r
;
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
G
t
;
.
.
.
E
X
P
E
C
T
_
C
A
L
L
(
f
o
o
, B
a
r
(
5
)
)
;
E
X
P
E
C
T
_
C
A
L
L
(
f
o
o
, B
a
r
(
G
t
(
1
0
)
)
)
.
T
i
m
e
s
(
A
n
y
N
u
m
b
e
r
(
)
)
;
A call to f
o
o
.
B
a
r
(
)that doesn't match any of the E
X
P
E
C
T
_
C
A
L
L
(
)statements will be an error.
Expecting Ordered Calls
Although an E
X
P
E
C
T
_
C
A
L
L
(
)statement defined earlier takes precedence when Google Mock tries to match a function call with an expectation,
by default calls don't have to happen in the order E
X
P
E
C
T
_
C
A
L
L
(
)statements are written. For example, if the arguments match the matchers in
the third E
X
P
E
C
T
_
C
A
L
L
(
)
, but not those in the first two, then the third expectation will be used.
If you would rather have all calls occur in the order of the expectations, put the E
X
P
E
C
T
_
C
A
L
L
(
)statements in a block where you define a
variable of type I
n
S
e
q
u
e
n
c
e
:
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
_
;
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
I
n
S
e
q
u
e
n
c
e
;
{
I
n
S
e
q
u
e
n
c
e s
;
E
X
P
E
C
T
_
C
A
L
L
(
f
o
o
, D
o
T
h
i
s
(
5
)
)
;
E
X
P
E
C
T
_
C
A
L
L
(
b
a
r
, D
o
T
h
a
t
(
_
)
)
.
T
i
m
e
s
(
2
)
;
E
X
P
E
C
T
_
C
A
L
L
(
f
o
o
, D
o
T
h
i
s
(
6
)
)
;
}
In this example, we expect a call to f
o
o
.
D
o
T
h
i
s
(
5
)
, followed by two calls to b
a
r
.
D
o
T
h
a
t
(
)where the argument can be anything, which are in
turn followed by a call to f
o
o
.
D
o
T
h
i
s
(
6
)
. If a call occurred out­of­order, Google Mock will report an error.
Expecting Partially Ordered Calls
Sometimes requiring everything to occur in a predetermined order can lead to brittle tests. For example, we may care about Aoccurring before
both Band C
, but aren't interested in the relative order of Band C
. In this case, the test should reflect our real intent, instead of being overly
constraining.
Google Mock allows you to impose an arbitrary DAG (directed acyclic graph) on the calls. One way to express the DAG is to use the After
clause of E
X
P
E
C
T
_
C
A
L
L
.
Another way is via the I
n
S
e
q
u
e
n
c
e
(
)clause (not the same as the I
n
S
e
q
u
e
n
c
eclass), which we borrowed from jMock 2. It's less flexible than
A
f
t
e
r
(
)
, but more convenient when you have long chains of sequential calls, as it doesn't require you to come up with different names for the
expectations in the chains. Here's how it works:
If we view E
X
P
E
C
T
_
C
A
L
L
(
)statements as nodes in a graph, and add an edge from node A to node B wherever A must occur before B, we can
get a DAG. We use the term "sequence" to mean a directed path in this DAG. Now, if we decompose the DAG into sequences, we just need to
know which sequences each E
X
P
E
C
T
_
C
A
L
L
(
)belongs to in order to be able to reconstruct the orginal DAG.
So, to specify the partial order on the expectations we need to do two things: first to define some S
e
q
u
e
n
c
eobjects, and then for each
E
X
P
E
C
T
_
C
A
L
L
(
)say which S
e
q
u
e
n
c
eobjects it is part of. Expectations in the same sequence must occur in the order they are written. For
example,
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
S
e
q
u
e
n
c
e
;
S
e
q
u
e
n
c
e s
1
, s
2
;
8/23/13 CookBook - googlemock - Google C++ Mocking Framework Cookbook - Google C++ Mocking Framework - Google Project Hosting
https://code.google.com/p/googlemock/wiki/CookBook 18/41
S
e
q
u
e
n
c
e s
1
, s
2
;
E
X
P
E
C
T
_
C
A
L
L
(
f
o
o
, A
(
)
)
.
I
n
S
e
q
u
e
n
c
e
(
s
1
, s
2
)
;
E
X
P
E
C
T
_
C
A
L
L
(
b
a
r
, B
(
)
)
.
I
n
S
e
q
u
e
n
c
e
(
s
1
)
;
E
X
P
E
C
T
_
C
A
L
L
(
b
a
r
, C
(
)
)
.
I
n
S
e
q
u
e
n
c
e
(
s
2
)
;
E
X
P
E
C
T
_
C
A
L
L
(
f
o
o
, D
(
)
)
.
I
n
S
e
q
u
e
n
c
e
(
s
2
)
;
specifies the following DAG (where s
1is A -
> B
, and s
2is `A ­> C ­> D`):
+
-
-
-
> B
|
A -
-
-
|
|
+
-
-
-
> C -
-
-
> D
This means that A must occur before B and C, and C must occur before D. There's no restriction about the order other than these.
Controlling When an Expectation Retires
When a mock method is called, Google Mock only consider expectations that are still active. An expectation is active when created, and
becomes inactive (aka retires) when a call that has to occur later has occurred. For example, in
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
_
;
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
S
e
q
u
e
n
c
e
;
S
e
q
u
e
n
c
e s
1
, s
2
;
E
X
P
E
C
T
_
C
A
L
L
(
l
o
g
, L
o
g
(
W
A
R
N
I
N
G
, _
, "
F
i
l
e t
o
o l
a
r
g
e
.
"
)
) /
/ #
1
.
T
i
m
e
s
(
A
n
y
N
u
m
b
e
r
(
)
)
.
I
n
S
e
q
u
e
n
c
e
(
s
1
, s
2
)
;
E
X
P
E
C
T
_
C
A
L
L
(
l
o
g
, L
o
g
(
W
A
R
N
I
N
G
, _
, "
D
a
t
a s
e
t i
s e
m
p
t
y
.
"
)
) /
/ #
2
.
I
n
S
e
q
u
e
n
c
e
(
s
1
)
;
E
X
P
E
C
T
_
C
A
L
L
(
l
o
g
, L
o
g
(
W
A
R
N
I
N
G
, _
, "
U
s
e
r n
o
t f
o
u
n
d
.
"
)
) /
/ #
3
.
I
n
S
e
q
u
e
n
c
e
(
s
2
)
;
as soon as either #2 or #3 is matched, #1 will retire. If a warning "
F
i
l
e t
o
o l
a
r
g
e
.
"is logged after this, it will be an error.
Note that an expectation doesn't retire automatically when it's saturated. For example,
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
_
;
.
.
.
E
X
P
E
C
T
_
C
A
L
L
(
l
o
g
, L
o
g
(
W
A
R
N
I
N
G
, _
, _
)
)
; /
/ #
1
E
X
P
E
C
T
_
C
A
L
L
(
l
o
g
, L
o
g
(
W
A
R
N
I
N
G
, _
, "
F
i
l
e t
o
o l
a
r
g
e
.
"
)
)
; /
/ #
2
says that there will be exactly one warning with the message `"File too large."`. If the second warning contains this message too, #2 will match
again and result in an upper­bound­violated error.
If this is not what you want, you can ask an expectation to retire as soon as it becomes saturated:
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
_
;
.
.
.
E
X
P
E
C
T
_
C
A
L
L
(
l
o
g
, L
o
g
(
W
A
R
N
I
N
G
, _
, _
)
)
; /
/ #
1
E
X
P
E
C
T
_
C
A
L
L
(
l
o
g
, L
o
g
(
W
A
R
N
I
N
G
, _
, "
F
i
l
e t
o
o l
a
r
g
e
.
"
)
) /
/ #
2
.
R
e
t
i
r
e
s
O
n
S
a
t
u
r
a
t
i
o
n
(
)
;
Here #2 can be used only once, so if you have two warnings with the message "
F
i
l
e t
o
o l
a
r
g
e
.
"
, the first will match #2 and the second will
match #1 ­ there will be no error.
Using Actions
Returning References from Mock Methods
If a mock function's return type is a reference, you need to use R
e
t
u
r
n
R
e
f
(
)instead of R
e
t
u
r
n
(
)to return a result:
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
R
e
t
u
r
n
R
e
f
;
c
l
a
s
s M
o
c
k
F
o
o : p
u
b
l
i
c F
o
o {
8/23/13 CookBook - googlemock - Google C++ Mocking Framework Cookbook - Google C++ Mocking Framework - Google Project Hosting
https://code.google.com/p/googlemock/wiki/CookBook 19/41
c
l
a
s
s M
o
c
k
F
o
o : p
u
b
l
i
c F
o
o {
p
u
b
l
i
c
:
M
O
C
K
_
M
E
T
H
O
D
0
(
G
e
t
B
a
r
, B
a
r
&
(
)
)
;
}
;
.
.
.
M
o
c
k
F
o
o f
o
o
;
B
a
r b
a
r
;
E
X
P
E
C
T
_
C
A
L
L
(
f
o
o
, G
e
t
B
a
r
(
)
)
.
W
i
l
l
O
n
c
e
(
R
e
t
u
r
n
R
e
f
(
b
a
r
)
)
;
Returning Live Values from Mock Methods
The R
e
t
u
r
n
(
x
)action saves a copy of xwhen the action is created, and always returns the same value whenever it's executed. Sometimes you
may want to instead return the live value of x(i.e. its value at the time when the action is executed.).
If the mock function's return type is a reference, you can do it using R
e
t
u
r
n
R
e
f
(
x
)
, as shown in the previous recipe ("Returning References from
Mock Methods"). However, Google Mock doesn't let you use R
e
t
u
r
n
R
e
f
(
)in a mock function whose return type is not a reference, as doing that
usually indicates a user error. So, what shall you do?
You may be tempted to try B
y
R
e
f
(
)
:
u
s
i
n
g t
e
s
t
i
n
g
:
:
B
y
R
e
f
;
u
s
i
n
g t
e
s
t
i
n
g
:
:
R
e
t
u
r
n
;
c
l
a
s
s M
o
c
k
F
o
o : p
u
b
l
i
c F
o
o {
p
u
b
l
i
c
:
M
O
C
K
_
M
E
T
H
O
D
0
(
G
e
t
V
a
l
u
e
, i
n
t
(
)
)
;
}
;
.
.
.
i
n
t x = 0
;
M
o
c
k
F
o
o f
o
o
;
E
X
P
E
C
T
_
C
A
L
L
(
f
o
o
, G
e
t
V
a
l
u
e
(
)
)
.
W
i
l
l
R
e
p
e
a
t
e
d
l
y
(
R
e
t
u
r
n
(
B
y
R
e
f
(
x
)
)
)
;
x = 4
2
;
E
X
P
E
C
T
_
E
Q
(
4
2
, f
o
o
.
G
e
t
V
a
l
u
e
(
)
)
;
Unfortunately, it doesn't work here. The above code will fail with error:
V
a
l
u
e o
f
: f
o
o
.
G
e
t
V
a
l
u
e
(
)
A
c
t
u
a
l
: 0
E
x
p
e
c
t
e
d
: 4
2
The reason is that R
e
t
u
r
n
(
v
a
l
u
e
)converts v
a
l
u
eto the actual return type of the mock function at the time when the action is created, not
when it is executed. (This behavior was chosen for the action to be safe when v
a
l
u
eis a proxy object that references some temporary objects.)
As a result, B
y
R
e
f
(
x
)is converted to an i
n
tvalue (instead of a c
o
n
s
t i
n
t
&
) when the expectation is set, and R
e
t
u
r
n
(
B
y
R
e
f
(
x
)
)will always
return 0.
R
e
t
u
r
n
P
o
i
n
t
e
e
(
p
o
i
n
t
e
r
)was provided to solve this problem specifically. It returns the value pointed to by p
o
i
n
t
e
rat the time the action is
executed:
u
s
i
n
g t
e
s
t
i
n
g
:
:
R
e
t
u
r
n
P
o
i
n
t
e
e
;
.
.
.
i
n
t x = 0
;
M
o
c
k
F
o
o f
o
o
;
E
X
P
E
C
T
_
C
A
L
L
(
f
o
o
, G
e
t
V
a
l
u
e
(
)
)
.
W
i
l
l
R
e
p
e
a
t
e
d
l
y
(
R
e
t
u
r
n
P
o
i
n
t
e
e
(
&
x
)
)
; /
/ N
o
t
e t
h
e & h
e
r
e
.
x = 4
2
;
E
X
P
E
C
T
_
E
Q
(
4
2
, f
o
o
.
G
e
t
V
a
l
u
e
(
)
)
; /
/ T
h
i
s w
i
l
l s
u
c
c
e
e
d n
o
w
.
Combining Actions
Want to do more than one thing when a function is called? That's fine. D
o
A
l
l
(
)allow you to do sequence of actions every time. Only the return
value of the last action in the sequence will be used.
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
D
o
A
l
l
;
c
l
a
s
s M
o
c
k
F
o
o : p
u
b
l
i
c F
o
o {
p
u
b
l
i
c
:
M
O
C
K
_
M
E
T
H
O
D
1
(
B
a
r
, b
o
o
l
(
i
n
t n
)
)
;
}
;
.
.
.
8/23/13 CookBook - googlemock - Google C++ Mocking Framework Cookbook - Google C++ Mocking Framework - Google Project Hosting
https://code.google.com/p/googlemock/wiki/CookBook 20/41
.
.
.
E
X
P
E
C
T
_
C
A
L
L
(
f
o
o
, B
a
r
(
_
)
)
.
W
i
l
l
O
n
c
e
(
D
o
A
l
l
(
a
c
t
i
o
n
_
1
,
a
c
t
i
o
n
_
2
,
.
.
.
a
c
t
i
o
n
_
n
)
)
;
Mocking Side Effects
Sometimes a method exhibits its effect not via returning a value but via side effects. For example, it may change some global state or modify an
output argument. To mock side effects, in general you can define your own action by implementing :
:
t
e
s
t
i
n
g
:
:
A
c
t
i
o
n
I
n
t
e
r
f
a
c
e
.
If all you need to do is to change an output argument, the built­in S
e
t
A
r
g
P
o
i
n
t
e
e
(
)action is convenient:
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
S
e
t
A
r
g
P
o
i
n
t
e
e
;
c
l
a
s
s M
o
c
k
M
u
t
a
t
o
r : p
u
b
l
i
c M
u
t
a
t
o
r {
p
u
b
l
i
c
:
M
O
C
K
_
M
E
T
H
O
D
2
(
M
u
t
a
t
e
, v
o
i
d
(
b
o
o
l m
u
t
a
t
e
, i
n
t
* v
a
l
u
e
)
)
;
.
.
.
}
;
.
.
.
M
o
c
k
M
u
t
a
t
o
r m
u
t
a
t
o
r
;
E
X
P
E
C
T
_
C
A
L
L
(
m
u
t
a
t
o
r
, M
u
t
a
t
e
(
t
r
u
e
, _
)
)
.
W
i
l
l
O
n
c
e
(
S
e
t
A
r
g
P
o
i
n
t
e
e
<
1
>
(
5
)
)
;
In this example, when m
u
t
a
t
o
r
.
M
u
t
a
t
e
(
)is called, we will assign 5 to the i
n
tvariable pointed to by argument #1 (0­based).
S
e
t
A
r
g
P
o
i
n
t
e
e
(
)conveniently makes an internal copy of the value you pass to it, removing the need to keep the value in scope and alive. The
implication however is that the value must have a copy constructor and assignment operator.
If the mock method also needs to return a value as well, you can chain S
e
t
A
r
g
P
o
i
n
t
e
e
(
)with R
e
t
u
r
n
(
)using D
o
A
l
l
(
)
:
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
_
;
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
R
e
t
u
r
n
;
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
S
e
t
A
r
g
P
o
i
n
t
e
e
;
c
l
a
s
s M
o
c
k
M
u
t
a
t
o
r : p
u
b
l
i
c M
u
t
a
t
o
r {
p
u
b
l
i
c
:
.
.
.
M
O
C
K
_
M
E
T
H
O
D
1
(
M
u
t
a
t
e
I
n
t
, b
o
o
l
(
i
n
t
* v
a
l
u
e
)
)
;
}
;
.
.
.
M
o
c
k
M
u
t
a
t
o
r m
u
t
a
t
o
r
;
E
X
P
E
C
T
_
C
A
L
L
(
m
u
t
a
t
o
r
, M
u
t
a
t
e
I
n
t
(
_
)
)
.
W
i
l
l
O
n
c
e
(
D
o
A
l
l
(
S
e
t
A
r
g
P
o
i
n
t
e
e
<
0
>
(
5
)
,
R
e
t
u
r
n
(
t
r
u
e
)
)
)
;
If the output argument is an array, use the S
e
t
A
r
r
a
y
A
r
g
u
m
e
n
t
<
N
>
(
f
i
r
s
t
, l
a
s
t
)action instead. It copies the elements in source range
[
f
i
r
s
t
, l
a
s
t
)to the array pointed to by the N
­th (0­based) argument:
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
N
o
t
N
u
l
l
;
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
S
e
t
A
r
r
a
y
A
r
g
u
m
e
n
t
;
c
l
a
s
s M
o
c
k
A
r
r
a
y
M
u
t
a
t
o
r : p
u
b
l
i
c A
r
r
a
y
M
u
t
a
t
o
r {
p
u
b
l
i
c
:
M
O
C
K
_
M
E
T
H
O
D
2
(
M
u
t
a
t
e
, v
o
i
d
(
i
n
t
* v
a
l
u
e
s
, i
n
t n
u
m
_
v
a
l
u
e
s
)
)
;
.
.
.
}
;
.
.
.
M
o
c
k
A
r
r
a
y
M
u
t
a
t
o
r m
u
t
a
t
o
r
;
i
n
t v
a
l
u
e
s
[
5
] = { 1
, 2
, 3
, 4
, 5 }
;
E
X
P
E
C
T
_
C
A
L
L
(
m
u
t
a
t
o
r
, M
u
t
a
t
e
(
N
o
t
N
u
l
l
(
)
, 5
)
)
.
W
i
l
l
O
n
c
e
(
S
e
t
A
r
r
a
y
A
r
g
u
m
e
n
t
<
0
>
(
v
a
l
u
e
s
, v
a
l
u
e
s + 5
)
)
;
This also works when the argument is an output iterator:
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
_
;
8/23/13 CookBook - googlemock - Google C++ Mocking Framework Cookbook - Google C++ Mocking Framework - Google Project Hosting
https://code.google.com/p/googlemock/wiki/CookBook 21/41
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
_
;
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
S
e
A
r
r
a
y
A
r
g
u
m
e
n
t
;
c
l
a
s
s M
o
c
k
R
o
l
o
d
e
x : p
u
b
l
i
c R
o
l
o
d
e
x {
p
u
b
l
i
c
:
M
O
C
K
_
M
E
T
H
O
D
1
(
G
e
t
N
a
m
e
s
, v
o
i
d
(
s
t
d
:
:
b
a
c
k
_
i
n
s
e
r
t
_
i
t
e
r
a
t
o
r
<
v
e
c
t
o
r
<
s
t
r
i
n
g
> >
)
)
;
.
.
.
}
;
.
.
.
M
o
c
k
R
o
l
o
d
e
x r
o
l
o
d
e
x
;
v
e
c
t
o
r
<
s
t
r
i
n
g
> n
a
m
e
s
;
n
a
m
e
s
.
p
u
s
h
_
b
a
c
k
(
"
G
e
o
r
g
e
"
)
;
n
a
m
e
s
.
p
u
s
h
_
b
a
c
k
(
"
J
o
h
n
"
)
;
n
a
m
e
s
.
p
u
s
h
_
b
a
c
k
(
"
T
h
o
m
a
s
"
)
;
E
X
P
E
C
T
_
C
A
L
L
(
r
o
l
o
d
e
x
, G
e
t
N
a
m
e
s
(
_
)
)
.
W
i
l
l
O
n
c
e
(
S
e
t
A
r
r
a
y
A
r
g
u
m
e
n
t
<
0
>
(
n
a
m
e
s
.
b
e
g
i
n
(
)
, n
a
m
e
s
.
e
n
d
(
)
)
)
;
Changing a Mock Object's Behavior Based on the State
If you expect a call to change the behavior of a mock object, you can use :
:
t
e
s
t
i
n
g
:
:
I
n
S
e
q
u
e
n
c
eto specify different behaviors before and
after the call:
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
I
n
S
e
q
u
e
n
c
e
;
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
R
e
t
u
r
n
;
.
.
.
{
I
n
S
e
q
u
e
n
c
e s
e
q
;
E
X
P
E
C
T
_
C
A
L
L
(
m
y
_
m
o
c
k
, I
s
D
i
r
t
y
(
)
)
.
W
i
l
l
R
e
p
e
a
t
e
d
l
y
(
R
e
t
u
r
n
(
t
r
u
e
)
)
;
E
X
P
E
C
T
_
C
A
L
L
(
m
y
_
m
o
c
k
, F
l
u
s
h
(
)
)
;
E
X
P
E
C
T
_
C
A
L
L
(
m
y
_
m
o
c
k
, I
s
D
i
r
t
y
(
)
)
.
W
i
l
l
R
e
p
e
a
t
e
d
l
y
(
R
e
t
u
r
n
(
f
a
l
s
e
)
)
;
}
m
y
_
m
o
c
k
.
F
l
u
s
h
I
f
D
i
r
t
y
(
)
;
This makes m
y
_
m
o
c
k
.
I
s
D
i
r
t
y
(
)return t
r
u
ebefore m
y
_
m
o
c
k
.
F
l
u
s
h
(
)is called and return f
a
l
s
eafterwards.
If the behavior change is more complex, you can store the effects in a variable and make a mock method get its return value from that variable:
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
_
;
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
S
a
v
e
A
r
g
;
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
R
e
t
u
r
n
;
A
C
T
I
O
N
_
P
(
R
e
t
u
r
n
P
o
i
n
t
e
e
, p
) { r
e
t
u
r
n *
p
; }
.
.
.
i
n
t p
r
e
v
i
o
u
s
_
v
a
l
u
e = 0
;
E
X
P
E
C
T
_
C
A
L
L
(
m
y
_
m
o
c
k
, G
e
t
P
r
e
v
V
a
l
u
e
(
)
)
.
W
i
l
l
R
e
p
e
a
t
e
d
l
y
(
R
e
t
u
r
n
P
o
i
n
t
e
e
(
&
p
r
e
v
i
o
u
s
_
v
a
l
u
e
)
)
;
E
X
P
E
C
T
_
C
A
L
L
(
m
y
_
m
o
c
k
, U
p
d
a
t
e
V
a
l
u
e
(
_
)
)
.
W
i
l
l
R
e
p
e
a
t
e
d
l
y
(
S
a
v
e
A
r
g
<
0
>
(
&
p
r
e
v
i
o
u
s
_
v
a
l
u
e
)
)
;
m
y
_
m
o
c
k
.
D
o
S
o
m
e
t
h
i
n
g
T
o
U
p
d
a
t
e
V
a
l
u
e
(
)
;
Here m
y
_
m
o
c
k
.
G
e
t
P
r
e
v
V
a
l
u
e
(
)will always return the argument of the last U
p
d
a
t
e
V
a
l
u
e
(
)call.
Setting the Default Value for a Return Type
If a mock method's return type is a built­in C++ type or pointer, by default it will return 0 when invoked. You only need to specify an action if this
default value doesn't work for you.
Sometimes, you may want to change this default value, or you may want to specify a default value for types Google Mock doesn't know about.
You can do this using the :
:
t
e
s
t
i
n
g
:
:
D
e
f
a
u
l
t
V
a
l
u
eclass template:
c
l
a
s
s M
o
c
k
F
o
o : p
u
b
l
i
c F
o
o {
p
u
b
l
i
c
:
M
O
C
K
_
M
E
T
H
O
D
0
(
C
a
l
c
u
l
a
t
e
B
a
r
, B
a
r
(
)
)
;
}
;
.
.
.
B
a
r d
e
f
a
u
l
t
_
b
a
r
;
/
/ S
e
t
s t
h
e d
e
f
a
u
l
t r
e
t
u
r
n v
a
l
u
e f
o
r t
y
p
e B
a
r
.
8/23/13 CookBook - googlemock - Google C++ Mocking Framework Cookbook - Google C++ Mocking Framework - Google Project Hosting
https://code.google.com/p/googlemock/wiki/CookBook 22/41
/
/ S
e
t
s t
h
e d
e
f
a
u
l
t r
e
t
u
r
n v
a
l
u
e f
o
r t
y
p
e B
a
r
.
D
e
f
a
u
l
t
V
a
l
u
e
<
B
a
r
>
:
:
S
e
t
(
d
e
f
a
u
l
t
_
b
a
r
)
;
M
o
c
k
F
o
o f
o
o
;
/
/ W
e d
o
n
'
t n
e
e
d t
o s
p
e
c
i
f
y a
n a
c
t
i
o
n h
e
r
e
, a
s t
h
e d
e
f
a
u
l
t
/
/ r
e
t
u
r
n v
a
l
u
e w
o
r
k
s f
o
r u
s
.
E
X
P
E
C
T
_
C
A
L
L
(
f
o
o
, C
a
l
c
u
l
a
t
e
B
a
r
(
)
)
;
f
o
o
.
C
a
l
c
u
l
a
t
e
B
a
r
(
)
; /
/ T
h
i
s s
h
o
u
l
d r
e
t
u
r
n d
e
f
a
u
l
t
_
b
a
r
.
/
/ U
n
s
e
t
s t
h
e d
e
f
a
u
l
t r
e
t
u
r
n v
a
l
u
e
.
D
e
f
a
u
l
t
V
a
l
u
e
<
B
a
r
>
:
:
C
l
e
a
r
(
)
;
Please note that changing the default value for a type can make you tests hard to understand. We recommend you to use this feature
judiciously. For example, you may want to make sure the S
e
t
(
)and C
l
e
a
r
(
)calls are right next to the code that uses your mock.
Setting the Default Actions for a Mock Method
You've learned how to change the default value of a given type. However, this may be too coarse for your purpose: perhaps you have two mock
methods with the same return type and you want them to have different behaviors. The O
N
_
C
A
L
L
(
)macro allows you to customize your mock's
behavior at the method level:
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
_
;
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
A
n
y
N
u
m
b
e
r
;
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
G
t
;
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
R
e
t
u
r
n
;
.
.
.
O
N
_
C
A
L
L
(
f
o
o
, S
i
g
n
(
_
)
)
.
W
i
l
l
B
y
D
e
f
a
u
l
t
(
R
e
t
u
r
n
(
-
1
)
)
;
O
N
_
C
A
L
L
(
f
o
o
, S
i
g
n
(
0
)
)
.
W
i
l
l
B
y
D
e
f
a
u
l
t
(
R
e
t
u
r
n
(
0
)
)
;
O
N
_
C
A
L
L
(
f
o
o
, S
i
g
n
(
G
t
(
0
)
)
)
.
W
i
l
l
B
y
D
e
f
a
u
l
t
(
R
e
t
u
r
n
(
1
)
)
;
E
X
P
E
C
T
_
C
A
L
L
(
f
o
o
, S
i
g
n
(
_
)
)
.
T
i
m
e
s
(
A
n
y
N
u
m
b
e
r
(
)
)
;
f
o
o
.
S
i
g
n
(
5
)
; /
/ T
h
i
s s
h
o
u
l
d r
e
t
u
r
n 1
.
f
o
o
.
S
i
g
n
(
-
9
)
; /
/ T
h
i
s s
h
o
u
l
d r
e
t
u
r
n -
1
.
f
o
o
.
S
i
g
n
(
0
)
; /
/ T
h
i
s s
h
o
u
l
d r
e
t
u
r
n 0
.
As you may have guessed, when there are more than one O
N
_
C
A
L
L
(
)statements, the news order take precedence over the older ones. In other
words, the last one that matches the function arguments will be used. This matching order allows you to set up the common behavior in a mock
object's constructor or the test fixture's set­up phase and specialize the mock's behavior later.
Using Functions/Methods/Functors as Actions
If the built­in actions don't suit you, you can easily use an existing function, method, or functor as an action:
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
_
;
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
I
n
v
o
k
e
;
c
l
a
s
s M
o
c
k
F
o
o : p
u
b
l
i
c F
o
o {
p
u
b
l
i
c
:
M
O
C
K
_
M
E
T
H
O
D
2
(
S
u
m
, i
n
t
(
i
n
t x
, i
n
t y
)
)
;
M
O
C
K
_
M
E
T
H
O
D
1
(
C
o
m
p
l
e
x
J
o
b
, b
o
o
l
(
i
n
t x
)
)
;
}
;
i
n
t C
a
l
c
u
l
a
t
e
S
u
m
(
i
n
t x
, i
n
t y
) { r
e
t
u
r
n x + y
; }
c
l
a
s
s H
e
l
p
e
r {
p
u
b
l
i
c
:
b
o
o
l C
o
m
p
l
e
x
J
o
b
(
i
n
t x
)
;
}
;
.
.
.
M
o
c
k
F
o
o f
o
o
;
H
e
l
p
e
r h
e
l
p
e
r
;
E
X
P
E
C
T
_
C
A
L
L
(
f
o
o
, S
u
m
(
_
, _
)
)
.
W
i
l
l
O
n
c
e
(
I
n
v
o
k
e
(
C
a
l
c
u
l
a
t
e
S
u
m
)
)
;
E
X
P
E
C
T
_
C
A
L
L
(
f
o
o
, C
o
m
p
l
e
x
J
o
b
(
_
)
)
8/23/13 CookBook - googlemock - Google C++ Mocking Framework Cookbook - Google C++ Mocking Framework - Google Project Hosting
https://code.google.com/p/googlemock/wiki/CookBook 23/41
E
X
P
E
C
T
_
C
A
L
L
(
f
o
o
, C
o
m
p
l
e
x
J
o
b
(
_
)
)
.
W
i
l
l
O
n
c
e
(
I
n
v
o
k
e
(
&
h
e
l
p
e
r
, &
H
e
l
p
e
r
:
:
C
o
m
p
l
e
x
J
o
b
)
)
;
f
o
o
.
S
u
m
(
5
, 6
)
; /
/ I
n
v
o
k
e
s C
a
l
c
u
l
a
t
e
S
u
m
(
5
, 6
)
.
f
o
o
.
C
o
m
p
l
e
x
J
o
b
(
1
0
)
; /
/ I
n
v
o
k
e
s h
e
l
p
e
r
.
C
o
m
p
l
e
x
J
o
b
(
1
0
)
;
The only requirement is that the type of the function, etc must be compatible with the signature of the mock function, meaning that the latter's
arguments can be implicitly converted to the corresponding arguments of the former, and the former's return type can be implicitly converted to
that of the latter. So, you can invoke something whose type is not exactly the same as the mock function, as long as it's safe to do so ­ nice,
huh?
Invoking a Function/Method/Functor Without Arguments
I
n
v
o
k
e
(
)is very useful for doing actions that are more complex. It passes the mock function's arguments to the function or functor being
invoked such that the callee has the full context of the call to work with. If the invoked function is not interested in some or all of the arguments,
it can simply ignore them.
Yet, a common pattern is that a test author wants to invoke a function without the arguments of the mock function. I
n
v
o
k
e
(
)allows her to do
that using a wrapper function that throws away the arguments before invoking an underlining nullary function. Needless to say, this can be
tedious and obscures the intent of the test.
I
n
v
o
k
e
W
i
t
h
o
u
t
A
r
g
s
(
)solves this problem. It's like I
n
v
o
k
e
(
)except that it doesn't pass the mock function's arguments to the callee. Here's an
example:
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
_
;
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
I
n
v
o
k
e
W
i
t
h
o
u
t
A
r
g
s
;
c
l
a
s
s M
o
c
k
F
o
o : p
u
b
l
i
c F
o
o {
p
u
b
l
i
c
:
M
O
C
K
_
M
E
T
H
O
D
1
(
C
o
m
p
l
e
x
J
o
b
, b
o
o
l
(
i
n
t n
)
)
;
}
;
b
o
o
l J
o
b
1
(
) { .
.
. }
.
.
.
M
o
c
k
F
o
o f
o
o
;
E
X
P
E
C
T
_
C
A
L
L
(
f
o
o
, C
o
m
p
l
e
x
J
o
b
(
_
)
)
.
W
i
l
l
O
n
c
e
(
I
n
v
o
k
e
W
i
t
h
o
u
t
A
r
g
s
(
J
o
b
1
)
)
;
f
o
o
.
C
o
m
p
l
e
x
J
o
b
(
1
0
)
; /
/ I
n
v
o
k
e
s J
o
b
1
(
)
.
Invoking an Argument of the Mock Function
Sometimes a mock function will receive a function pointer or a functor (in other words, a "callable") as an argument, e.g.
c
l
a
s
s M
o
c
k
F
o
o : p
u
b
l
i
c F
o
o {
p
u
b
l
i
c
:
M
O
C
K
_
M
E
T
H
O
D
2
(
D
o
T
h
i
s
, b
o
o
l
(
i
n
t n
, b
o
o
l (
*
f
p
)
(
i
n
t
)
)
)
;
}
;
and you may want to invoke this callable argument:
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
_
;
.
.
.
M
o
c
k
F
o
o f
o
o
;
E
X
P
E
C
T
_
C
A
L
L
(
f
o
o
, D
o
T
h
i
s
(
_
, _
)
)
.
W
i
l
l
O
n
c
e
(
.
.
.
)
;
/
/ W
i
l
l e
x
e
c
u
t
e (
*
f
p
)
(
5
)
, w
h
e
r
e f
p i
s t
h
e
/
/ s
e
c
o
n
d a
r
g
u
m
e
n
t D
o
T
h
i
s
(
) r
e
c
e
i
v
e
s
.
Arghh, you need to refer to a mock function argument but C++ has no lambda (yet), so you have to define your own action. :­( Or do you really?
Well, Google Mock has an action to solve exactly this problem:
I
n
v
o
k
e
A
r
g
u
m
e
n
t
<
N
>
(
a
r
g
_
1
, a
r
g
_
2
, .
.
.
, a
r
g
_
m
)
will invoke the N
­th (0­based) argument the mock function receives, with a
r
g
_
1
, a
r
g
_
2
, ..., and a
r
g
_
m
. No matter if the argument is a function
pointer or a functor, Google Mock handles them both.
With that, you could write:
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
_
;
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
I
n
v
o
k
e
A
r
g
u
m
e
n
t
;
8/23/13 CookBook - googlemock - Google C++ Mocking Framework Cookbook - Google C++ Mocking Framework - Google Project Hosting
https://code.google.com/p/googlemock/wiki/CookBook 24/41
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
I
n
v
o
k
e
A
r
g
u
m
e
n
t
;
.
.
.
E
X
P
E
C
T
_
C
A
L
L
(
f
o
o
, D
o
T
h
i
s
(
_
, _
)
)
.
W
i
l
l
O
n
c
e
(
I
n
v
o
k
e
A
r
g
u
m
e
n
t
<
1
>
(
5
)
)
;
/
/ W
i
l
l e
x
e
c
u
t
e (
*
f
p
)
(
5
)
, w
h
e
r
e f
p i
s t
h
e
/
/ s
e
c
o
n
d a
r
g
u
m
e
n
t D
o
T
h
i
s
(
) r
e
c
e
i
v
e
s
.
What if the callable takes an argument by reference? No problem ­ just wrap it inside B
y
R
e
f
(
)
:
.
.
.
M
O
C
K
_
M
E
T
H
O
D
1
(
B
a
r
, b
o
o
l
(
b
o
o
l (
*
f
p
)
(
i
n
t
, c
o
n
s
t H
e
l
p
e
r
&
)
)
)
;
.
.
.
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
_
;
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
B
y
R
e
f
;
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
I
n
v
o
k
e
A
r
g
u
m
e
n
t
;
.
.
.
M
o
c
k
F
o
o f
o
o
;
H
e
l
p
e
r h
e
l
p
e
r
;
.
.
.
E
X
P
E
C
T
_
C
A
L
L
(
f
o
o
, B
a
r
(
_
)
)
.
W
i
l
l
O
n
c
e
(
I
n
v
o
k
e
A
r
g
u
m
e
n
t
<
0
>
(
5
, B
y
R
e
f
(
h
e
l
p
e
r
)
)
)
;
/
/ B
y
R
e
f
(
h
e
l
p
e
r
) g
u
a
r
a
n
t
e
e
s t
h
a
t a r
e
f
e
r
e
n
c
e t
o h
e
l
p
e
r
, n
o
t a c
o
p
y o
f i
t
,
/
/ w
i
l
l b
e p
a
s
s
e
d t
o t
h
e c
a
l
l
a
b
l
e
.
What if the callable takes an argument by reference and we do not wrap the argument in B
y
R
e
f
(
)
? Then I
n
v
o
k
e
A
r
g
u
m
e
n
t
(
)will make a copy of
the argument, and pass a reference to the copy, instead of a reference to the original value, to the callable. This is especially handy when the
argument is a temporary value:
.
.
.
M
O
C
K
_
M
E
T
H
O
D
1
(
D
o
T
h
a
t
, b
o
o
l
(
b
o
o
l (
*
f
)
(
c
o
n
s
t d
o
u
b
l
e
& x
, c
o
n
s
t s
t
r
i
n
g
& s
)
)
)
;
.
.
.
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
_
;
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
I
n
v
o
k
e
A
r
g
u
m
e
n
t
;
.
.
.
M
o
c
k
F
o
o f
o
o
;
.
.
.
E
X
P
E
C
T
_
C
A
L
L
(
f
o
o
, D
o
T
h
a
t
(
_
)
)
.
W
i
l
l
O
n
c
e
(
I
n
v
o
k
e
A
r
g
u
m
e
n
t
<
0
>
(
5
.
0
, s
t
r
i
n
g
(
"
H
i
"
)
)
)
;
/
/ W
i
l
l e
x
e
c
u
t
e (
*
f
)
(
5
.
0
, s
t
r
i
n
g
(
"
H
i
"
)
)
, w
h
e
r
e f i
s t
h
e f
u
n
c
t
i
o
n p
o
i
n
t
e
r
/
/ D
o
T
h
a
t
(
) r
e
c
e
i
v
e
s
. N
o
t
e t
h
a
t t
h
e v
a
l
u
e
s 5
.
0 a
n
d s
t
r
i
n
g
(
"
H
i
"
) a
r
e
/
/ t
e
m
p
o
r
a
r
y a
n
d d
e
a
d o
n
c
e t
h
e E
X
P
E
C
T
_
C
A
L
L
(
) s
t
a
t
e
m
e
n
t f
i
n
i
s
h
e
s
. Y
e
t
/
/ i
t
'
s f
i
n
e t
o p
e
r
f
o
r
m t
h
i
s a
c
t
i
o
n l
a
t
e
r
, s
i
n
c
e a c
o
p
y o
f t
h
e v
a
l
u
e
s
/
/ a
r
e k
e
p
t i
n
s
i
d
e t
h
e I
n
v
o
k
e
A
r
g
u
m
e
n
t a
c
t
i
o
n
.
Ignoring an Action's Result
Sometimes you have an action that returns something, but you need an action that returns v
o
i
d(perhaps you want to use it in a mock function
that returns v
o
i
d
, or perhaps it needs to be used in D
o
A
l
l
(
)and it's not the last in the list). I
g
n
o
r
e
R
e
s
u
l
t
(
)lets you do that. For example:
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
_
;
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
I
n
v
o
k
e
;
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
R
e
t
u
r
n
;
i
n
t P
r
o
c
e
s
s
(
c
o
n
s
t M
y
D
a
t
a
& d
a
t
a
)
;
s
t
r
i
n
g D
o
S
o
m
e
t
h
i
n
g
(
)
;
c
l
a
s
s M
o
c
k
F
o
o : p
u
b
l
i
c F
o
o {
p
u
b
l
i
c
:
M
O
C
K
_
M
E
T
H
O
D
1
(
A
b
c
, v
o
i
d
(
c
o
n
s
t M
y
D
a
t
a
& d
a
t
a
)
)
;
M
O
C
K
_
M
E
T
H
O
D
0
(
X
y
z
, b
o
o
l
(
)
)
;
}
;
.
.
.
M
o
c
k
F
o
o f
o
o
;
E
X
P
E
C
T
_
C
A
L
L
(
f
o
o
, A
b
c
(
_
)
)
/
/ .
W
i
l
l
O
n
c
e
(
I
n
v
o
k
e
(
P
r
o
c
e
s
s
)
)
;
/
/ T
h
e a
b
o
v
e l
i
n
e w
o
n
'
t c
o
m
p
i
l
e a
s P
r
o
c
e
s
s
(
) r
e
t
u
r
n
s i
n
t b
u
t A
b
c
(
) n
e
e
d
s
/
/ t
o r
e
t
u
r
n v
o
i
d
.
.
W
i
l
l
O
n
c
e
(
I
g
n
o
r
e
R
e
s
u
l
t
(
I
n
v
o
k
e
(
P
r
o
c
e
s
s
)
)
)
;
8/23/13 CookBook - googlemock - Google C++ Mocking Framework Cookbook - Google C++ Mocking Framework - Google Project Hosting
https://code.google.com/p/googlemock/wiki/CookBook 25/41
.
W
i
l
l
O
n
c
e
(
I
g
n
o
r
e
R
e
s
u
l
t
(
I
n
v
o
k
e
(
P
r
o
c
e
s
s
)
)
)
;
E
X
P
E
C
T
_
C
A
L
L
(
f
o
o
, X
y
z
(
)
)
.
W
i
l
l
O
n
c
e
(
D
o
A
l
l
(
I
g
n
o
r
e
R
e
s
u
l
t
(
I
n
v
o
k
e
(
D
o
S
o
m
e
t
h
i
n
g
)
)
,
/
/ I
g
n
o
r
e
s t
h
e s
t
r
i
n
g D
o
S
o
m
e
t
h
i
n
g
(
) r
e
t
u
r
n
s
.
R
e
t
u
r
n
(
t
r
u
e
)
)
)
;
Note that you cannot use I
g
n
o
r
e
R
e
s
u
l
t
(
)on an action that already returns v
o
i
d
. Doing so will lead to ugly compiler errors.
Selecting an Action's Arguments
Say you have a mock function F
o
o
(
)that takes seven arguments, and you have a custom action that you want to invoke when F
o
o
(
)is called.
Trouble is, the custom action only wants three arguments:
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
_
;
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
I
n
v
o
k
e
;
.
.
.
M
O
C
K
_
M
E
T
H
O
D
7
(
F
o
o
, b
o
o
l
(
b
o
o
l v
i
s
i
b
l
e
, c
o
n
s
t s
t
r
i
n
g
& n
a
m
e
, i
n
t x
, i
n
t y
,
c
o
n
s
t m
a
p
<
p
a
i
r
<
i
n
t
, i
n
t
>
, d
o
u
b
l
e
>
& w
e
i
g
h
t
,
d
o
u
b
l
e m
i
n
_
w
e
i
g
h
t
, d
o
u
b
l
e m
a
x
_
w
i
g
h
t
)
)
;
.
.
.
b
o
o
l I
s
V
i
s
i
b
l
e
I
n
Q
u
a
d
r
a
n
t
1
(
b
o
o
l v
i
s
i
b
l
e
, i
n
t x
, i
n
t y
) {
r
e
t
u
r
n v
i
s
i
b
l
e &
& x >
= 0 &
& y >
= 0
;
}
.
.
.
E
X
P
E
C
T
_
C
A
L
L
(
m
o
c
k
, F
o
o
(
_
, _
, _
, _
, _
, _
, _
)
)
.
W
i
l
l
O
n
c
e
(
I
n
v
o
k
e
(
I
s
V
i
s
i
b
l
e
I
n
Q
u
a
d
r
a
n
t
1
)
)
; /
/ U
h
, w
o
n
'
t c
o
m
p
i
l
e
. :
-
(
To please the compiler God, you can to define an "adaptor" that has the same signature as F
o
o
(
)and calls the custom action with the right
arguments:
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
_
;
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
I
n
v
o
k
e
;
b
o
o
l M
y
I
s
V
i
s
i
b
l
e
I
n
Q
u
a
d
r
a
n
t
1
(
b
o
o
l v
i
s
i
b
l
e
, c
o
n
s
t s
t
r
i
n
g
& n
a
m
e
, i
n
t x
, i
n
t y
,
c
o
n
s
t m
a
p
<
p
a
i
r
<
i
n
t
, i
n
t
>
, d
o
u
b
l
e
>
& w
e
i
g
h
t
,
d
o
u
b
l
e m
i
n
_
w
e
i
g
h
t
, d
o
u
b
l
e m
a
x
_
w
i
g
h
t
) {
r
e
t
u
r
n I
s
V
i
s
i
b
l
e
I
n
Q
u
a
d
r
a
n
t
1
(
v
i
s
i
b
l
e
, x
, y
)
;
}
.
.
.
E
X
P
E
C
T
_
C
A
L
L
(
m
o
c
k
, F
o
o
(
_
, _
, _
, _
, _
, _
, _
)
)
.
W
i
l
l
O
n
c
e
(
I
n
v
o
k
e
(
M
y
I
s
V
i
s
i
b
l
e
I
n
Q
u
a
d
r
a
n
t
1
)
)
; /
/ N
o
w i
t w
o
r
k
s
.
But isn't this awkward?
Google Mock provides a generic action adaptor, so you can spend your time minding more important business than writing your own adaptors.
Here's the syntax:
W
i
t
h
A
r
g
s
<
N
1
, N
2
, .
.
.
, N
k
>
(
a
c
t
i
o
n
)
creates an action that passes the arguments of the mock function at the given indices (0­based) to the inner a
c
t
i
o
nand performs it. Using
W
i
t
h
A
r
g
s
, our original example can be written as:
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
_
;
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
I
n
v
o
k
e
;
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
W
i
t
h
A
r
g
s
;
.
.
.
E
X
P
E
C
T
_
C
A
L
L
(
m
o
c
k
, F
o
o
(
_
, _
, _
, _
, _
, _
, _
)
)
.
W
i
l
l
O
n
c
e
(
W
i
t
h
A
r
g
s
<
0
, 2
, 3
>
(
I
n
v
o
k
e
(
I
s
V
i
s
i
b
l
e
I
n
Q
u
a
d
r
a
n
t
1
)
)
)
;
/
/ N
o n
e
e
d t
o d
e
f
i
n
e y
o
u
r o
w
n a
d
a
p
t
o
r
.
For better readability, Google Mock also gives you:
W
i
t
h
o
u
t
A
r
g
s
(
a
c
t
i
o
n
)when the inner a
c
t
i
o
ntakes no argument, and
W
i
t
h
A
r
g
<
N
>
(
a
c
t
i
o
n
)(no safter A
r
g
) when the inner a
c
t
i
o
ntakes one argument.
As you may have realized, I
n
v
o
k
e
W
i
t
h
o
u
t
A
r
g
s
(
.
.
.
)is just syntactic sugar for W
i
t
h
o
u
t
A
r
g
s
(
I
n
o
v
k
e
(
.
.
.
)
)
.
Here are more tips:
8/23/13 CookBook - googlemock - Google C++ Mocking Framework Cookbook - Google C++ Mocking Framework - Google Project Hosting
https://code.google.com/p/googlemock/wiki/CookBook 26/41
Here are more tips:
The inner action used in W
i
t
h
A
r
g
sand friends does not have to be I
n
v
o
k
e
(
)­­ it can be anything.
You can repeat an argument in the argument list if necessary, e.g. W
i
t
h
A
r
g
s
<
2
, 3
, 3
, 5
>
(
.
.
.
)
.
You can change the order of the arguments, e.g. W
i
t
h
A
r
g
s
<
3
, 2
, 1
>
(
.
.
.
)
.
The types of the selected arguments do not have to match the signature of the inner action exactly. It works as long as they can be
implicitly converted to the corresponding arguments of the inner action. For example, if the 4­th argument of the mock function is an i
n
tand
m
y
_
a
c
t
i
o
ntakes a d
o
u
b
l
e
, W
i
t
h
A
r
g
<
4
>
(
m
y
_
a
c
t
i
o
n
)will work.
Ignoring Arguments in Action Functions
The selecting­an­action's­arguments recipe showed us one way to make a mock function and an action with incompatible argument lists fit
together. The downside is that wrapping the action in W
i
t
h
A
r
g
s
<
.
.
.
>
(
)can get tedious for people writing the tests.
If you are defining a function, method, or functor to be used with I
n
v
o
k
e
*
(
)
, and you are not interested in some of its arguments, an alternative
to W
i
t
h
A
r
g
sis to declare the uninteresting arguments as U
n
u
s
e
d
. This makes the definition less cluttered and less fragile in case the types of
the uninteresting arguments change. It could also increase the chance the action function can be reused. For example, given
M
O
C
K
_
M
E
T
H
O
D
3
(
F
o
o
, d
o
u
b
l
e
(
c
o
n
s
t s
t
r
i
n
g
& l
a
b
e
l
, d
o
u
b
l
e x
, d
o
u
b
l
e y
)
)
;
M
O
C
K
_
M
E
T
H
O
D
3
(
B
a
r
, d
o
u
b
l
e
(
i
n
t i
n
d
e
x
, d
o
u
b
l
e x
, d
o
u
b
l
e y
)
)
;
instead of
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
_
;
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
I
n
v
o
k
e
;
d
o
u
b
l
e D
i
s
t
a
n
c
e
T
o
O
r
i
g
i
n
W
i
t
h
L
a
b
e
l
(
c
o
n
s
t s
t
r
i
n
g
& l
a
b
e
l
, d
o
u
b
l
e x
, d
o
u
b
l
e y
) {
r
e
t
u
r
n s
q
r
t
(
x
*
x + y
*
y
)
;
}
d
o
u
b
l
e D
i
s
t
a
n
c
e
T
o
O
r
i
g
i
n
W
i
t
h
I
n
d
e
x
(
i
n
t i
n
d
e
x
, d
o
u
b
l
e x
, d
o
u
b
l
e y
) {
r
e
t
u
r
n s
q
r
t
(
x
*
x + y
*
y
)
;
}
.
.
.
E
X
E
P
C
T
_
C
A
L
L
(
m
o
c
k
, F
o
o
(
"
a
b
c
"
, _
, _
)
)
.
W
i
l
l
O
n
c
e
(
I
n
v
o
k
e
(
D
i
s
t
a
n
c
e
T
o
O
r
i
g
i
n
W
i
t
h
L
a
b
e
l
)
)
;
E
X
E
P
C
T
_
C
A
L
L
(
m
o
c
k
, B
a
r
(
5
, _
, _
)
)
.
W
i
l
l
O
n
c
e
(
I
n
v
o
k
e
(
D
i
s
t
a
n
c
e
T
o
O
r
i
g
i
n
W
i
t
h
I
n
d
e
x
)
)
;
you could write
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
_
;
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
I
n
v
o
k
e
;
u
s
i
n
g :
:
t
e
s
t
i
n
g
:
:
U
n
u
s
e
d
;
d
o
u
b
l
e D
i
s
t
a
n
c
e
T
o
O
r
i
g
i
n
(
U
n
u
s
e
d
, d
o
u
b
l
e x
, d
o
u
b
l
e y
) {
r
e
t
u
r
n s
q
r
t
(
x
*
x + y
*
y
)
;
}
.
.
.
E
X
E
P
C
T
_
C
A
L
L
(
m
o
c
k
, F
o
o
(
"
a
b
c
"
, _
, _
)
)
.
W
i
l
l
O
n
c
e
(
I
n
v
o
k
e
(
D
i
s
t
a
n
c
e
T
o
O
r
i
g
i
n
)
)
;
E
X
E
P
C
T
_
C
A
L
L
(
m
o
c
k
, B
a
r
(
5
, _
, _
)
)
.
W
i
l
l
O
n
c
e
(
I
n
v
o
k
e
(
D
i
s
t
a
n
c
e
T
o
O
r
i
g
i
n
)
)
;
Sharing Actions
Just like matchers, a Google Mock action object consists of a pointer to a ref­counted implementation object. Therefore copying actions is also
allowed and very efficient. When the last action that references the implementation object dies, the implementation object will be deleted.
If you have some complex action that you want to use again and again, you may not have to build it from scratch everytime. If the action doesn't
have an internal state (i.e. if it always does the same thing no matter how many times it has been called), you can assign it to an action variable
and use that variable repeatedly. For example:
A
c
t
i
o
n
<
b
o
o
l
(
i
n
t
*
)
> s
e
t
_
f
l
a
g = D
o
A
l
l
(
S
e
t
A
r
g
P
o
i
n
t
e
e
<
0
>
(
5
)
,
R
e
t
u
r
n
(
t
r
u
e
)
)
;
.
.
. u
s
e s
e
t
_
f
l
a
g i
n .
W
i
l
l
O
n
c
e
(
) a
n
d .
W
i
l
l
R
e
p
e
a
t
e
d
l
y
(
) .
.
.
However, if the action has its own state, you may be surprised if you share the action object. Suppose you have an action factory
I
n
c
r
e
m
e
n
t
C
o
u
n
t
e
r
(
i
n
i
t
)which creates an action that increments and returns a counter whose initial value is i
n
i
t
, using two actions created
8/23/13 CookBook - googlemock - Google C++ Mocking Framework Cookbook - Google C++ Mocking Framework - Google Project Hosting
https://code.google.com/p/googlemock/wiki/CookBook 27/41
from the same expression and using a shared action will exihibit different behaviors. Example:
E
X
P
E
C
T
_
C
A
L
L
(
f
o
o
, D
o
T
h
i
s
(
)
)
.
W
i
l
l
R
e
p
e
a
t
e
d
l
y
(
I
n
c
r
e
m
e
n
t
C
o
u
n
t
e
r
(
0
)
)
;
E
X
P
E
C
T
_
C
A
L
L
(
f
o
o
, D
o
T
h
a
t
(
)
)
.
W
i
l
l
R
e
p
e
a
t
e
d
l
y
(
I
n
c
r
e
m
e
n
t
C
o
u
n
t
e
r
(
0
)
)
;
f
o
o
.
D
o
T
h
i
s
(
)
; /
/ R
e
t
u
r
n
s 1
.
f
o
o
.
D
o
T
h
i
s
(
)
; /
/ R
e
t
u
r
n
s 2
.
f
o
o
.
D
o
T
h
a
t
(
)
; /
/ R
e
t
u
r
n
s 1 - B
l
a
h
(
) u
s
e
s a d
i
f
f
e
r
e
n
t
/
/ c
o
u
n
t
e
r t
h
a
n B
a
r
(
)
'
s
.
versus
A
c
t
i
o
n
<
i
n
t
(
)
> i
n
c
r
e
m
e
n
t = I
n
c
r
e
m
e
n
t
C
o
u
n
t
e
r
(
0
)
;
E
X
P
E
C
T
_
C
A
L
L
(
f
o
o
, D
o
T
h
i
s
(
)
)
.
W
i
l
l
R
e
p
e
a
t
e
d
l
y
(
i
n
c
r
e
m
e
n
t
)
;
E
X
P
E
C
T
_
C
A
L
L
(
f
o
o
, D
o
T
h
a
t
(
)
)
.
W
i
l
l
R
e
p
e
a
t
e
d
l
y
(
i
n
c
r
e
m
e
n
t
)
;
f
o
o
.
D
o
T
h
i
s
(
)
; /
/ R
e
t
u
r
n
s 1
.
f
o
o
.
D
o
T
h
i
s
(
)
; /
/ R
e
t
u
r
n
s 2
.
f
o
o
.
D
o
T
h
a
t
(
)
; /
/ R
e
t
u
r
n
s 3 - t
h
e c
o
u
n
t
e
r i
s s
h
a
r
e
d
.
Misc Recipes on Using Google Mock
Making the Compilation Faster
Believe it or not, the vast majority of the time spent on compiling a mock class is in generating its constructor and destructor, as they perform
non­trivial tasks (e.g. verification of the expectations). What's more, mock methods with different signatures have different types and thus their
constructors/destructors need to be generated by the compiler separately. As a result, if you mock many different types of methods, compiling
your mock class can get really slow.
If you are experiencing slow compilation, you can move the definition of your mock class' constructor and destructor out of the class body and
into a .
c
p
pfile. This way, even if you #
i
n
c
l
u
d
eyour mock class in N files, the compiler only needs to generate its constructor and destructor
once, resulting in a much faster compilation.
Let's illustrate the idea using an example. Here's the definition of a mock class before applying this recipe:
/
/ F
i
l
e m
o
c
k
_
f
o
o
.
h
.
.
.
.
c
l
a
s
s M
o
c
k
F
o
o : p
u
b
l
i
c F
o
o {
p
u
b
l
i
c
:
/
/ S
i
n
c
e w
e d
o
n
'
t d
e
c
l
a
r
e t
h
e c
o
n
s
t
r
u
c
t
o
r o
r t
h
e d
e
s
t
r
u
c
t
o
r
,
/
/ t
h
e c
o
m
p
i
l
e
r w
i
l
l g
e
n
e
r
a
t
e t
h
e
m i
n e
v
e
r
y t
r
a
n
s
l
a
t
i
o
n u
n
i
t
/
/ w
h
e
r
e t
h
i
s m
o
c
k c
l
a
s
s i
s u
s
e
d
.
M
O
C
K
_
M
E
T
H
O
D
0
(
D
o
T
h
i
s
, i
n
t
(
)
)
;
M
O
C
K
_
M
E
T
H
O
D
1
(
D
o
T
h
a
t
, b
o
o
l
(
c
o
n
s
t c
h
a
r
* s
t
r
)
)
;
.
.
. m
o
r
e m
o
c
k m
e
t
h
o
d
s .
.
.
}
;
After the change, it would look like:
/
/ F
i
l
e m
o
c
k
_
f
o
o
.
h
.
.
.
.
c
l
a
s
s M
o
c
k
F
o
o : p
u
b
l
i
c F
o
o {
p
u
b
l
i
c
:
/
/ T
h
e c
o
n
s
t
r
u
c
t
o
r a
n
d d
e
s
t
r
u
c
t
o
r a
r
e d
e
c
l
a
r
e
d
, b
u
t n
o
t d
e
f
i
n
e
d
, h
e
r
e
.
M
o
c
k
F
o
o
(
)
;
v
i
r
t
u
a
l ~
M
o
c
k
F
o
o
(
)
;
M
O
C
K
_
M
E
T
H
O
D
0
(
D
o
T
h
i
s
, i
n
t
(
)
)
;
M
O
C
K
_
M
E
T
H
O
D
1
(
D
o
T
h
a
t
, b
o
o
l
(
c
o
n
s
t c
h
a
r
* s
t
r
)
)
;
.
.
. m
o
r
e m
o
c
k m
e
t
h
o
d
s .
.
.
}
;
and
/
/ F
i
l
e m
o
c
k
_
f
o
o
.
c
p
p
.
#
i
n
c
l
u
d
e "
p
a
t
h
/
t
o
/
m
o
c
k
_
f
o
o
.
h
"
gmock-cookbook.pdf
gmock-cookbook.pdf
gmock-cookbook.pdf
gmock-cookbook.pdf
gmock-cookbook.pdf
gmock-cookbook.pdf
gmock-cookbook.pdf
gmock-cookbook.pdf
gmock-cookbook.pdf
gmock-cookbook.pdf
gmock-cookbook.pdf
gmock-cookbook.pdf
gmock-cookbook.pdf
gmock-cookbook.pdf

More Related Content

Similar to gmock-cookbook.pdf

Google mock for dummies
Google mock for dummiesGoogle mock for dummies
Google mock for dummies
Tony Nguyen
 
Google mock for dummies
Google mock for dummiesGoogle mock for dummies
Google mock for dummies
Harry Potter
 
Google mock for dummies
Google mock for dummiesGoogle mock for dummies
Google mock for dummies
Young Alista
 
Google mock for dummies
Google mock for dummiesGoogle mock for dummies
Google mock for dummies
Luis Goldster
 
Google mock for dummies
Google mock for dummiesGoogle mock for dummies
Google mock for dummies
Fraboni Ec
 
Google mock for dummies
Google mock for dummiesGoogle mock for dummies
Google mock for dummies
James Wong
 

Similar to gmock-cookbook.pdf (20)

Go testunderthehood
Go testunderthehoodGo testunderthehood
Go testunderthehood
 
Google mock for dummies
Google mock for dummiesGoogle mock for dummies
Google mock for dummies
 
Google mock for dummies
Google mock for dummiesGoogle mock for dummies
Google mock for dummies
 
Google mock for dummies
Google mock for dummiesGoogle mock for dummies
Google mock for dummies
 
Google mock for dummies
Google mock for dummiesGoogle mock for dummies
Google mock for dummies
 
Google mock for dummies
Google mock for dummiesGoogle mock for dummies
Google mock for dummies
 
Google mock for dummies
Google mock for dummiesGoogle mock for dummies
Google mock for dummies
 
Google mock for dummies
Google mock for dummiesGoogle mock for dummies
Google mock for dummies
 
Boetticher Presentation Promise 2008v2
Boetticher Presentation Promise 2008v2Boetticher Presentation Promise 2008v2
Boetticher Presentation Promise 2008v2
 
Applying Statistical Modeling and Machine Learning to Perform Time-Series For...
Applying Statistical Modeling and Machine Learning to Perform Time-Series For...Applying Statistical Modeling and Machine Learning to Perform Time-Series For...
Applying Statistical Modeling and Machine Learning to Perform Time-Series For...
 
50+ Ways to Improve Your Classroom With Technology v 4.0
50+ Ways to Improve Your Classroom With Technology v 4.050+ Ways to Improve Your Classroom With Technology v 4.0
50+ Ways to Improve Your Classroom With Technology v 4.0
 
Object Oriented Software Development revision slide
Object Oriented Software Development revision slide Object Oriented Software Development revision slide
Object Oriented Software Development revision slide
 
A Brighter Web Meetup: Our Favorite WordPress Plugins and Tools
A Brighter Web Meetup: Our Favorite WordPress Plugins and ToolsA Brighter Web Meetup: Our Favorite WordPress Plugins and Tools
A Brighter Web Meetup: Our Favorite WordPress Plugins and Tools
 
Innovation Track AWS Cloud Experience Argentina - Democratizing Artificial In...
Innovation Track AWS Cloud Experience Argentina - Democratizing Artificial In...Innovation Track AWS Cloud Experience Argentina - Democratizing Artificial In...
Innovation Track AWS Cloud Experience Argentina - Democratizing Artificial In...
 
Untangling8
Untangling8Untangling8
Untangling8
 
Introduction to WordPress Hooks 2016
Introduction to WordPress Hooks 2016Introduction to WordPress Hooks 2016
Introduction to WordPress Hooks 2016
 
Online Machine Learning: introduction and examples
Online Machine Learning:  introduction and examplesOnline Machine Learning:  introduction and examples
Online Machine Learning: introduction and examples
 
PyLadies Talk: Learn to love the command line!
PyLadies Talk: Learn to love the command line!PyLadies Talk: Learn to love the command line!
PyLadies Talk: Learn to love the command line!
 
Extreme Google
Extreme GoogleExtreme Google
Extreme Google
 
a hands on guide to django
a hands on guide to djangoa hands on guide to django
a hands on guide to django
 

Recently uploaded

Top profile Call Girls In Thrissur [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Thrissur [ 7014168258 ] Call Me For Genuine Models ...Top profile Call Girls In Thrissur [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Thrissur [ 7014168258 ] Call Me For Genuine Models ...
nirzagarg
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN ABUDHABI,DUBAI MA...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN ABUDHABI,DUBAI MA...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN ABUDHABI,DUBAI MA...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN ABUDHABI,DUBAI MA...
Health
 
一比一原版(UdeM学位证书)蒙特利尔大学毕业证学历认证怎样办
一比一原版(UdeM学位证书)蒙特利尔大学毕业证学历认证怎样办一比一原版(UdeM学位证书)蒙特利尔大学毕业证学历认证怎样办
一比一原版(UdeM学位证书)蒙特利尔大学毕业证学历认证怎样办
ezgenuh
 
+97470301568>>buy vape oil,thc oil weed,hash and cannabis oil in qatar doha}}
+97470301568>>buy vape oil,thc oil weed,hash and cannabis oil in qatar doha}}+97470301568>>buy vape oil,thc oil weed,hash and cannabis oil in qatar doha}}
+97470301568>>buy vape oil,thc oil weed,hash and cannabis oil in qatar doha}}
Health
 
如何办理(NCL毕业证书)纽卡斯尔大学毕业证毕业证成绩单原版一比一
如何办理(NCL毕业证书)纽卡斯尔大学毕业证毕业证成绩单原版一比一如何办理(NCL毕业证书)纽卡斯尔大学毕业证毕业证成绩单原版一比一
如何办理(NCL毕业证书)纽卡斯尔大学毕业证毕业证成绩单原版一比一
avy6anjnd
 
如何办理美国华盛顿大学毕业证(UW毕业证书)毕业证成绩单原版一比一
如何办理美国华盛顿大学毕业证(UW毕业证书)毕业证成绩单原版一比一如何办理美国华盛顿大学毕业证(UW毕业证书)毕业证成绩单原版一比一
如何办理美国华盛顿大学毕业证(UW毕业证书)毕业证成绩单原版一比一
avy6anjnd
 
如何办理莱斯大学毕业证(Rice毕业证)毕业证成绩单原版一比一
如何办理莱斯大学毕业证(Rice毕业证)毕业证成绩单原版一比一如何办理莱斯大学毕业证(Rice毕业证)毕业证成绩单原版一比一
如何办理莱斯大学毕业证(Rice毕业证)毕业证成绩单原版一比一
avy6anjnd
 
如何办理伦敦商学院毕业证(LBS毕业证)毕业证成绩单原版一比一
如何办理伦敦商学院毕业证(LBS毕业证)毕业证成绩单原版一比一如何办理伦敦商学院毕业证(LBS毕业证)毕业证成绩单原版一比一
如何办理伦敦商学院毕业证(LBS毕业证)毕业证成绩单原版一比一
avy6anjnd
 
Top profile Call Girls In dewas [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In dewas [ 7014168258 ] Call Me For Genuine Models We ...Top profile Call Girls In dewas [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In dewas [ 7014168258 ] Call Me For Genuine Models We ...
gajnagarg
 

Recently uploaded (20)

Top profile Call Girls In Thrissur [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Thrissur [ 7014168258 ] Call Me For Genuine Models ...Top profile Call Girls In Thrissur [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Thrissur [ 7014168258 ] Call Me For Genuine Models ...
 
T.L.E 5S's (Seiri, Seiton, Seiso, Seiketsu, Shitsuke).pptx
T.L.E 5S's (Seiri, Seiton, Seiso, Seiketsu, Shitsuke).pptxT.L.E 5S's (Seiri, Seiton, Seiso, Seiketsu, Shitsuke).pptx
T.L.E 5S's (Seiri, Seiton, Seiso, Seiketsu, Shitsuke).pptx
 
Housewife Call Girl in Faridabad ₹7.5k Pick Up & Drop With Cash Payment #8168...
Housewife Call Girl in Faridabad ₹7.5k Pick Up & Drop With Cash Payment #8168...Housewife Call Girl in Faridabad ₹7.5k Pick Up & Drop With Cash Payment #8168...
Housewife Call Girl in Faridabad ₹7.5k Pick Up & Drop With Cash Payment #8168...
 
Is Your Volvo XC90 Displaying Anti-Skid Service Required Alert Here's Why
Is Your Volvo XC90 Displaying Anti-Skid Service Required Alert Here's WhyIs Your Volvo XC90 Displaying Anti-Skid Service Required Alert Here's Why
Is Your Volvo XC90 Displaying Anti-Skid Service Required Alert Here's Why
 
Seamless Driving Experience Premier Mini Cooper Clutch Solutions
Seamless Driving Experience Premier Mini Cooper Clutch SolutionsSeamless Driving Experience Premier Mini Cooper Clutch Solutions
Seamless Driving Experience Premier Mini Cooper Clutch Solutions
 
West Bengal Factories Rules, 1958.bfpptx
West Bengal Factories Rules, 1958.bfpptxWest Bengal Factories Rules, 1958.bfpptx
West Bengal Factories Rules, 1958.bfpptx
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN ABUDHABI,DUBAI MA...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN ABUDHABI,DUBAI MA...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN ABUDHABI,DUBAI MA...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN ABUDHABI,DUBAI MA...
 
一比一原版(UdeM学位证书)蒙特利尔大学毕业证学历认证怎样办
一比一原版(UdeM学位证书)蒙特利尔大学毕业证学历认证怎样办一比一原版(UdeM学位证书)蒙特利尔大学毕业证学历认证怎样办
一比一原版(UdeM学位证书)蒙特利尔大学毕业证学历认证怎样办
 
+97470301568>>buy vape oil,thc oil weed,hash and cannabis oil in qatar doha}}
+97470301568>>buy vape oil,thc oil weed,hash and cannabis oil in qatar doha}}+97470301568>>buy vape oil,thc oil weed,hash and cannabis oil in qatar doha}}
+97470301568>>buy vape oil,thc oil weed,hash and cannabis oil in qatar doha}}
 
如何办理(NCL毕业证书)纽卡斯尔大学毕业证毕业证成绩单原版一比一
如何办理(NCL毕业证书)纽卡斯尔大学毕业证毕业证成绩单原版一比一如何办理(NCL毕业证书)纽卡斯尔大学毕业证毕业证成绩单原版一比一
如何办理(NCL毕业证书)纽卡斯尔大学毕业证毕业证成绩单原版一比一
 
What Does The Engine Malfunction Reduced Power Message Mean For Your BMW X5
What Does The Engine Malfunction Reduced Power Message Mean For Your BMW X5What Does The Engine Malfunction Reduced Power Message Mean For Your BMW X5
What Does The Engine Malfunction Reduced Power Message Mean For Your BMW X5
 
如何办理美国华盛顿大学毕业证(UW毕业证书)毕业证成绩单原版一比一
如何办理美国华盛顿大学毕业证(UW毕业证书)毕业证成绩单原版一比一如何办理美国华盛顿大学毕业证(UW毕业证书)毕业证成绩单原版一比一
如何办理美国华盛顿大学毕业证(UW毕业证书)毕业证成绩单原版一比一
 
Washim Call Girls 📞9332606886 Call Girls in Washim Escorts service book now C...
Washim Call Girls 📞9332606886 Call Girls in Washim Escorts service book now C...Washim Call Girls 📞9332606886 Call Girls in Washim Escorts service book now C...
Washim Call Girls 📞9332606886 Call Girls in Washim Escorts service book now C...
 
如何办理莱斯大学毕业证(Rice毕业证)毕业证成绩单原版一比一
如何办理莱斯大学毕业证(Rice毕业证)毕业证成绩单原版一比一如何办理莱斯大学毕业证(Rice毕业证)毕业证成绩单原版一比一
如何办理莱斯大学毕业证(Rice毕业证)毕业证成绩单原版一比一
 
如何办理伦敦商学院毕业证(LBS毕业证)毕业证成绩单原版一比一
如何办理伦敦商学院毕业证(LBS毕业证)毕业证成绩单原版一比一如何办理伦敦商学院毕业证(LBS毕业证)毕业证成绩单原版一比一
如何办理伦敦商学院毕业证(LBS毕业证)毕业证成绩单原版一比一
 
Is Your Mercedes Benz Trunk Refusing To Close Here's What Might Be Wrong
Is Your Mercedes Benz Trunk Refusing To Close Here's What Might Be WrongIs Your Mercedes Benz Trunk Refusing To Close Here's What Might Be Wrong
Is Your Mercedes Benz Trunk Refusing To Close Here's What Might Be Wrong
 
SEM 922 MOTOR GRADER PARTS LIST, ALL WHEEL DRIVE
SEM 922 MOTOR GRADER PARTS LIST, ALL WHEEL DRIVESEM 922 MOTOR GRADER PARTS LIST, ALL WHEEL DRIVE
SEM 922 MOTOR GRADER PARTS LIST, ALL WHEEL DRIVE
 
Top profile Call Girls In dewas [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In dewas [ 7014168258 ] Call Me For Genuine Models We ...Top profile Call Girls In dewas [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In dewas [ 7014168258 ] Call Me For Genuine Models We ...
 
Stacey+= Dubai Calls Girls O525547819 Call Girls In Dubai
Stacey+= Dubai Calls Girls O525547819 Call Girls In DubaiStacey+= Dubai Calls Girls O525547819 Call Girls In Dubai
Stacey+= Dubai Calls Girls O525547819 Call Girls In Dubai
 
Marathi Call Girls Santacruz WhatsApp +91-9930687706, Best Service
Marathi Call Girls Santacruz WhatsApp +91-9930687706, Best ServiceMarathi Call Girls Santacruz WhatsApp +91-9930687706, Best Service
Marathi Call Girls Santacruz WhatsApp +91-9930687706, Best Service
 

gmock-cookbook.pdf

  • 1. 8/23/13 CookBook - googlemock - Google C++ Mocking Framework Cookbook - Google C++ Mocking Framework - Google Project Hosting https://code.google.com/p/googlemock/wiki/CookBook 1/41 Project Home Downloads Wiki Issues Source Updated Jul 29, 2013 by w...@google.com My favorites ▼ | Sign in googlemock Google C++ Mocking Framework Search projects Search Current pages for Search CookBook Google C++ Mocking Framework Cookbook Creating Mock Classes Mocking Private or Protected Methods Mocking Overloaded Methods Mocking Class Templates Mocking Nonvirtual Methods Mocking Free Functions The Nice, the Strict, and the Naggy Simplifying the Interface without Breaking Existing Code Alternative to Mocking Concrete Classes Delegating Calls to a Fake Delegating Calls to a Real Object Delegating Calls to a Parent Class Using Matchers Matching Argument Values Exactly Using Simple Matchers Combining Matchers Casting Matchers Selecting Between Overloaded Functions Performing Different Actions Based on the Arguments Matching Multiple Arguments as a Whole Using Matchers as Predicates Using Matchers in Google Test Assertions Using Predicates as Matchers Matching Arguments that Are Not Copyable Validating a Member of an Object Validating the Value Pointed to by a Pointer Argument Testing a Certain Property of an Object Matching Containers Sharing Matchers Setting Expectations Knowing When to Expect Ignoring Uninteresting Calls Disallowing Unexpected Calls Expecting Ordered Calls Expecting Partially Ordered Calls Controlling When an Expectation Retires Using Actions Returning References from Mock Methods Returning Live Values from Mock Methods Combining Actions Mocking Side Effects Changing a Mock Object's Behavior Based on the State Setting the Default Value for a Return Type Setting the Default Actions for a Mock Method Using Functions/Methods/Functors as Actions Invoking a Function/Method/Functor Without Arguments Invoking an Argument of the Mock Function Ignoring an Action's Result Selecting an Action's Arguments
  • 2. 8/23/13 CookBook - googlemock - Google C++ Mocking Framework Cookbook - Google C++ Mocking Framework - Google Project Hosting https://code.google.com/p/googlemock/wiki/CookBook 2/41 Selecting an Action's Arguments Ignoring Arguments in Action Functions Sharing Actions Misc Recipes on Using Google Mock Making the Compilation Faster Forcing a Verification Using Check Points Mocking Destructors Using Google Mock and Threads Controlling How Much Information Google Mock Prints Gaining Super Vision into Mock Calls Running Tests in Emacs Fusing Google Mock Source Files Extending Google Mock Writing New Matchers Quickly Writing New Parameterized Matchers Quickly Writing New Monomorphic Matchers Writing New Polymorphic Matchers Writing New Cardinalities Writing New Actions Quickly Writing New Parameterized Actions Quickly Restricting the Type of an Argument or Parameter in an ACTION Writing New Action Templates Quickly Using the ACTION Object's Type Writing New Monomorphic Actions Writing New Polymorphic Actions Teaching Google Mock How to Print Your Values You can find recipes for using Google Mock here. If you haven't yet, please read the ForDummies document first to make sure you understand the basics. Note: Google Mock lives in the t e s t i n gname space. For readability, it is recommended to write u s i n g : : t e s t i n g : : F o o ;once in your file before using the name F o odefined by Google Mock. We omit such u s i n gstatements in this page for brevity, but you should do it in your own code. Creating Mock Classes Mocking Private or Protected Methods You must always put a mock method definition (M O C K _ M E T H O D * ) in a p u b l i c :section of the mock class, regardless of the method being mocked being p u b l i c , p r o t e c t e d , or p r i v a t ein the base class. This allows O N _ C A L Land E X P E C T _ C A L Lto reference the mock function from outside of the mock class. (Yes, C++ allows a subclass to change the access level of a virtual function in the base class.) Example: c l a s s F o o { p u b l i c : . . . v i r t u a l b o o l T r a n s f o r m ( G a d g e t * g ) = 0 ; p r o t e c t e d : v i r t u a l v o i d R e s u m e ( ) ; p r i v a t e : v i r t u a l i n t G e t T i m e O u t ( ) ; } ; c l a s s M o c k F o o : p u b l i c F o o { p u b l i c : . . . M O C K _ M E T H O D 1 ( T r a n s f o r m , b o o l ( G a d g e t * g ) ) ; / / T h e f o l l o w i n g m u s t b e i n t h e p u b l i c s e c t i o n , e v e n t h o u g h t h e / / m e t h o d s a r e p r o t e c t e d o r p r i v a t e i n t h e b a s e c l a s s . M O C K _ M E T H O D 0 ( R e s u m e , v o i d ( ) ) ; M O C K _ M E T H O D 0 ( G e t T i m e O u t , i n t ( ) ) ; } ; Mocking Overloaded Methods
  • 3. 8/23/13 CookBook - googlemock - Google C++ Mocking Framework Cookbook - Google C++ Mocking Framework - Google Project Hosting https://code.google.com/p/googlemock/wiki/CookBook 3/41 You can mock overloaded functions as usual. No special attention is required: c l a s s F o o { . . . / / M u s t b e v i r t u a l a s w e ' l l i n h e r i t f r o m F o o . v i r t u a l ~ F o o ( ) ; / / O v e r l o a d e d o n t h e t y p e s a n d / o r n u m b e r s o f a r g u m e n t s . v i r t u a l i n t A d d ( E l e m e n t x ) ; v i r t u a l i n t A d d ( i n t t i m e s , E l e m e n t x ) ; / / O v e r l o a d e d o n t h e c o n s t - n e s s o f t h i s o b j e c t . v i r t u a l B a r & G e t B a r ( ) ; v i r t u a l c o n s t B a r & G e t B a r ( ) c o n s t ; } ; c l a s s M o c k F o o : p u b l i c F o o { . . . M O C K _ M E T H O D 1 ( A d d , i n t ( E l e m e n t x ) ) ; M O C K _ M E T H O D 2 ( A d d , i n t ( i n t t i m e s , E l e m e n t x ) ; M O C K _ M E T H O D 0 ( G e t B a r , B a r & ( ) ) ; M O C K _ C O N S T _ M E T H O D 0 ( G e t B a r , c o n s t B a r & ( ) ) ; } ; Note: if you don't mock all versions of the overloaded method, the compiler will give you a warning about some methods in the base class being hidden. To fix that, use u s i n gto bring them in scope: c l a s s M o c k F o o : p u b l i c F o o { . . . u s i n g F o o : : A d d ; M O C K _ M E T H O D 1 ( A d d , i n t ( E l e m e n t x ) ) ; / / W e d o n ' t w a n t t o m o c k i n t A d d ( i n t t i m e s , E l e m e n t x ) ; . . . } ; Mocking Class Templates To mock a class template, append _ Tto the M O C K _ *macros: t e m p l a t e < t y p e n a m e E l e m > c l a s s S t a c k I n t e r f a c e { . . . / / M u s t b e v i r t u a l a s w e ' l l i n h e r i t f r o m S t a c k I n t e r f a c e . v i r t u a l ~ S t a c k I n t e r f a c e ( ) ; v i r t u a l i n t G e t S i z e ( ) c o n s t = 0 ; v i r t u a l v o i d P u s h ( c o n s t E l e m & x ) = 0 ; } ; t e m p l a t e < t y p e n a m e E l e m > c l a s s M o c k S t a c k : p u b l i c S t a c k I n t e r f a c e < E l e m > { . . . M O C K _ C O N S T _ M E T H O D 0 _ T ( G e t S i z e , i n t ( ) ) ; M O C K _ M E T H O D 1 _ T ( P u s h , v o i d ( c o n s t E l e m & x ) ) ; } ; Mocking Nonvirtual Methods Google Mock can mock non­virtual functions to be used in what we call hi­perf dependency injection. In this case, instead of sharing a common base class with the real class, your mock class will be unrelated to the real class, but contain methods with the same signatures. The syntax for mocking non­virtual methods is the same as mocking virtual methods: / / A s i m p l e p a c k e t s t r e a m c l a s s . N o n e o f i t s m e m b e r s i s v i r t u a l . c l a s s C o n c r e t e P a c k e t S t r e a m { p u b l i c : v o i d A p p e n d P a c k e t ( P a c k e t * n e w _ p a c k e t ) ; c o n s t P a c k e t * G e t P a c k e t ( s i z e _ t p a c k e t _ n u m b e r ) c o n s t ; s i z e _ t N u m b e r O f P a c k e t s ( ) c o n s t ; . . .
  • 4. 8/23/13 CookBook - googlemock - Google C++ Mocking Framework Cookbook - Google C++ Mocking Framework - Google Project Hosting https://code.google.com/p/googlemock/wiki/CookBook 4/41 . . . } ; / / A m o c k p a c k e t s t r e a m c l a s s . I t i n h e r i t s f r o m n o o t h e r , b u t d e f i n e s / / G e t P a c k e t ( ) a n d N u m b e r O f P a c k e t s ( ) . c l a s s M o c k P a c k e t S t r e a m { p u b l i c : M O C K _ C O N S T _ M E T H O D 1 ( G e t P a c k e t , c o n s t P a c k e t * ( s i z e _ t p a c k e t _ n u m b e r ) ) ; M O C K _ C O N S T _ M E T H O D 0 ( N u m b e r O f P a c k e t s , s i z e _ t ( ) ) ; . . . } ; Note that the mock class doesn't define A p p e n d P a c k e t ( ) , unlike the real class. That's fine as long as the test doesn't need to call it. Next, you need a way to say that you want to use C o n c r e t e P a c k e t S t r e a min production code, and use M o c k P a c k e t S t r e a min tests. Since the functions are not virtual and the two classes are unrelated, you must specify your choice at compile time (as opposed to run time). One way to do it is to templatize your code that needs to use a packet stream. More specifically, you will give your code a template type argument for the type of the packet stream. In production, you will instantiate your template with C o n c r e t e P a c k e t S t r e a mas the type argument. In tests, you will instantiate the same template with M o c k P a c k e t S t r e a m . For example, you may write: t e m p l a t e < c l a s s P a c k e t S t r e a m > v o i d C r e a t e C o n n e c t i o n ( P a c k e t S t r e a m * s t r e a m ) { . . . } t e m p l a t e < c l a s s P a c k e t S t r e a m > c l a s s P a c k e t R e a d e r { p u b l i c : v o i d R e a d P a c k e t s ( P a c k e t S t r e a m * s t r e a m , s i z e _ t p a c k e t _ n u m ) ; } ; Then you can use C r e a t e C o n n e c t i o n < C o n c r e t e P a c k e t S t r e a m > ( )and P a c k e t R e a d e r < C o n c r e t e P a c k e t S t r e a m >in production code, and use C r e a t e C o n n e c t i o n < M o c k P a c k e t S t r e a m > ( )and P a c k e t R e a d e r < M o c k P a c k e t S t r e a m >in tests. M o c k P a c k e t S t r e a m m o c k _ s t r e a m ; E X P E C T _ C A L L ( m o c k _ s t r e a m , . . . ) . . . ; . . s e t m o r e e x p e c t a t i o n s o n m o c k _ s t r e a m . . . P a c k e t R e a d e r < M o c k P a c k e t S t r e a m > r e a d e r ( & m o c k _ s t r e a m ) ; . . . e x e r c i s e r e a d e r . . . Mocking Free Functions It's possible to use Google Mock to mock a free function (i.e. a C­style function or a static method). You just need to rewrite your code to use an interface (abstract class). Instead of calling a free function (say, O p e n F i l e ) directly, introduce an interface for it and have a concrete subclass that calls the free function: c l a s s F i l e I n t e r f a c e { p u b l i c : . . . v i r t u a l b o o l O p e n ( c o n s t c h a r * p a t h , c o n s t c h a r * m o d e ) = 0 ; } ; c l a s s F i l e : p u b l i c F i l e I n t e r f a c e { p u b l i c : . . . v i r t u a l b o o l O p e n ( c o n s t c h a r * p a t h , c o n s t c h a r * m o d e ) { r e t u r n O p e n F i l e ( p a t h , m o d e ) ; } } ; Your code should talk to F i l e I n t e r f a c eto open a file. Now it's easy to mock out the function. This may seem much hassle, but in practice you often have multiple related functions that you can put in the same interface, so the per­function syntactic overhead will be much lower. If you are concerned about the performance overhead incurred by virtual functions, and profiling confirms your concern, you can combine this with the recipe for mocking non­virtual methods. The Nice, the Strict, and the Naggy If a mock method has no E X P E C T _ C A L Lspec but is called, Google Mock will print a warning about the "uninteresting call". The rationale is:
  • 5. 8/23/13 CookBook - googlemock - Google C++ Mocking Framework Cookbook - Google C++ Mocking Framework - Google Project Hosting https://code.google.com/p/googlemock/wiki/CookBook 5/41 New methods may be added to an interface after a test is written. We shouldn't fail a test just because a method it doesn't know about is called. However, this may also mean there's a bug in the test, so Google Mock shouldn't be silent either. If the user believes these calls are harmless, he can add an E X P E C T _ C A L L ( )to suppress the warning. However, sometimes you may want to suppress all "uninteresting call" warnings, while sometimes you may want the opposite, i.e. to treat all of them as errors. Google Mock lets you make the decision on a per­mock­object basis. Suppose your test uses a mock class M o c k F o o : T E S T ( . . . ) { M o c k F o o m o c k _ f o o ; E X P E C T _ C A L L ( m o c k _ f o o , D o T h i s ( ) ) ; . . . c o d e t h a t u s e s m o c k _ f o o . . . } If a method of m o c k _ f o oother than D o T h i s ( )is called, it will be reported by Google Mock as a warning. However, if you rewrite your test to use N i c e M o c k < M o c k F o o >instead, the warning will be gone, resulting in a cleaner test output: u s i n g : : t e s t i n g : : N i c e M o c k ; T E S T ( . . . ) { N i c e M o c k < M o c k F o o > m o c k _ f o o ; E X P E C T _ C A L L ( m o c k _ f o o , D o T h i s ( ) ) ; . . . c o d e t h a t u s e s m o c k _ f o o . . . } N i c e M o c k < M o c k F o o >is a subclass of M o c k F o o , so it can be used wherever M o c k F o ois accepted. It also works if M o c k F o o 's constructor takes some arguments, as N i c e M o c k < M o c k F o o >"inherits" M o c k F o o 's constructors: u s i n g : : t e s t i n g : : N i c e M o c k ; T E S T ( . . . ) { N i c e M o c k < M o c k F o o > m o c k _ f o o ( 5 , " h i " ) ; / / C a l l s M o c k F o o ( 5 , " h i " ) . E X P E C T _ C A L L ( m o c k _ f o o , D o T h i s ( ) ) ; . . . c o d e t h a t u s e s m o c k _ f o o . . . } The usage of S t r i c t M o c kis similar, except that it makes all uninteresting calls failures: u s i n g : : t e s t i n g : : S t r i c t M o c k ; T E S T ( . . . ) { S t r i c t M o c k < M o c k F o o > m o c k _ f o o ; E X P E C T _ C A L L ( m o c k _ f o o , D o T h i s ( ) ) ; . . . c o d e t h a t u s e s m o c k _ f o o . . . / / T h e t e s t w i l l f a i l i f a m e t h o d o f m o c k _ f o o o t h e r t h a n D o T h i s ( ) / / i s c a l l e d . } There are some caveats though (I don't like them just as much as the next guy, but sadly they are side effects of C++'s limitations): 1. N i c e M o c k < M o c k F o o >and S t r i c t M o c k < M o c k F o o >only work for mock methods defined using the M O C K _ M E T H O D *family of macros directly in the M o c k F o oclass. If a mock method is defined in a base class of M o c k F o o , the "nice" or "strict" modifier may not affect it, depending on the compiler. In particular, nesting N i c e M o c kand S t r i c t M o c k(e.g. N i c e M o c k < S t r i c t M o c k < M o c k F o o > > ) is not supported. 2. The constructors of the base mock (M o c k F o o ) cannot have arguments passed by non­const reference, which happens to be banned by the Google C++ style guide. 3. During the constructor or destructor of M o c k F o o , the mock object is not nice or strict. This may cause surprises if the constructor or destructor calls a mock method on t h i sobject. (This behavior, however, is consistent with C++'s general rule: if a constructor or destructor calls a virtual method of t h i sobject, that method is treated as non­virtual. In other words, to the base class's constructor or destructor, t h i sobject behaves like an instance of the base class, not the derived class. This rule is required for safety. Otherwise a base constructor may use members of a derived class before they are initialized, or a base destructor may use members of a derived class after they have been destroyed.) Finally, you should be very cautious about when to use naggy or strict mocks, as they tend to make tests more brittle and harder to maintain. When you refactor your code without changing its externally visible behavior, ideally you should't need to update any tests. If your code interacts with a naggy mock, however, you may start to get spammed with warnings as the result of your change. Worse, if your code interacts with a strict mock, your tests may start to fail and you'll be forced to fix them. Our general recommendation is to use nice mocks (not yet the default) most of the time, use naggy mocks (the current default) when developing or debugging tests, and use strict mocks only as the last resort.
  • 6. 8/23/13 CookBook - googlemock - Google C++ Mocking Framework Cookbook - Google C++ Mocking Framework - Google Project Hosting https://code.google.com/p/googlemock/wiki/CookBook 6/41 Simplifying the Interface without Breaking Existing Code Sometimes a method has a long list of arguments that is mostly uninteresting. For example, c l a s s L o g S i n k { p u b l i c : . . . v i r t u a l v o i d s e n d ( L o g S e v e r i t y s e v e r i t y , c o n s t c h a r * f u l l _ f i l e n a m e , c o n s t c h a r * b a s e _ f i l e n a m e , i n t l i n e , c o n s t s t r u c t t m * t m _ t i m e , c o n s t c h a r * m e s s a g e , s i z e _ t m e s s a g e _ l e n ) = 0 ; } ; This method's argument list is lengthy and hard to work with (let's say that the m e s s a g eargument is not even 0­terminated). If we mock it as is, using the mock will be awkward. If, however, we try to simplify this interface, we'll need to fix all clients depending on it, which is often infeasible. The trick is to re­dispatch the method in the mock class: c l a s s S c o p e d M o c k L o g : p u b l i c L o g S i n k { p u b l i c : . . . v i r t u a l v o i d s e n d ( L o g S e v e r i t y s e v e r i t y , c o n s t c h a r * f u l l _ f i l e n a m e , c o n s t c h a r * b a s e _ f i l e n a m e , i n t l i n e , c o n s t t m * t m _ t i m e , c o n s t c h a r * m e s s a g e , s i z e _ t m e s s a g e _ l e n ) { / / W e a r e o n l y i n t e r e s t e d i n t h e l o g s e v e r i t y , f u l l f i l e n a m e , a n d / / l o g m e s s a g e . L o g ( s e v e r i t y , f u l l _ f i l e n a m e , s t d : : s t r i n g ( m e s s a g e , m e s s a g e _ l e n ) ) ; } / / I m p l e m e n t s t h e m o c k m e t h o d : / / / / v o i d L o g ( L o g S e v e r i t y s e v e r i t y , / / c o n s t s t r i n g & f i l e _ p a t h , / / c o n s t s t r i n g & m e s s a g e ) ; M O C K _ M E T H O D 3 ( L o g , v o i d ( L o g S e v e r i t y s e v e r i t y , c o n s t s t r i n g & f i l e _ p a t h , c o n s t s t r i n g & m e s s a g e ) ) ; } ; By defining a new mock method with a trimmed argument list, we make the mock class much more user­friendly. Alternative to Mocking Concrete Classes Often you may find yourself using classes that don't implement interfaces. In order to test your code that uses such a class (let's call it C o n c r e t e ), you may be tempted to make the methods of C o n c r e t evirtual and then mock it. Try not to do that. Making a non­virtual function virtual is a big decision. It creates an extension point where subclasses can tweak your class' behavior. This weakens your control on the class because now it's harder to maintain the class' invariants. You should make a function virtual only when there is a valid reason for a subclass to override it. Mocking concrete classes directly is problematic as it creates a tight coupling between the class and the tests ­ any small change in the class may invalidate your tests and make test maintenance a pain. To avoid such problems, many programmers have been practicing "coding to interfaces": instead of talking to the C o n c r e t eclass, your code would define an interface and talk to it. Then you implement that interface as an adaptor on top of C o n c r e t e . In tests, you can easily mock that interface to observe how your code is doing. This technique incurs some overhead: You pay the cost of virtual function calls (usually not a problem). There is more abstraction for the programmers to learn. However, it can also bring significant benefits in addition to better testability: C o n c r e t e 's API may not fit your problem domain very well, as you may not be the only client it tries to serve. By designing your own interface, you have a chance to tailor it to your need ­ you may add higher­level functionalities, rename stuff, etc instead of just trimming the class. This allows you to write your code (user of the interface) in a more natural way, which means it will be more readable, more maintainable, and you'll be more productive. If C o n c r e t e 's implementation ever has to change, you don't have to rewrite everywhere it is used. Instead, you can absorb the change in your implementation of the interface, and your other code and tests will be insulated from this change.
  • 7. 8/23/13 CookBook - googlemock - Google C++ Mocking Framework Cookbook - Google C++ Mocking Framework - Google Project Hosting https://code.google.com/p/googlemock/wiki/CookBook 7/41 Some people worry that if everyone is practicing this technique, they will end up writing lots of redundant code. This concern is totally understandable. However, there are two reasons why it may not be the case: Different projects may need to use C o n c r e t ein different ways, so the best interfaces for them will be different. Therefore, each of them will have its own domain­specific interface on top of C o n c r e t e , and they will not be the same code. If enough projects want to use the same interface, they can always share it, just like they have been sharing C o n c r e t e . You can check in the interface and the adaptor somewhere near C o n c r e t e(perhaps in a c o n t r i bsub­directory) and let many projects use it. You need to weigh the pros and cons carefully for your particular problem, but I'd like to assure you that the Java community has been practicing this for a long time and it's a proven effective technique applicable in a wide variety of situations. :­) Delegating Calls to a Fake Some times you have a non­trivial fake implementation of an interface. For example: c l a s s F o o { p u b l i c : v i r t u a l ~ F o o ( ) { } v i r t u a l c h a r D o T h i s ( i n t n ) = 0 ; v i r t u a l v o i d D o T h a t ( c o n s t c h a r * s , i n t * p ) = 0 ; } ; c l a s s F a k e F o o : p u b l i c F o o { p u b l i c : v i r t u a l c h a r D o T h i s ( i n t n ) { r e t u r n ( n > 0 ) ? ' + ' : ( n < 0 ) ? ' - ' : ' 0 ' ; } v i r t u a l v o i d D o T h a t ( c o n s t c h a r * s , i n t * p ) { * p = s t r l e n ( s ) ; } } ; Now you want to mock this interface such that you can set expectations on it. However, you also want to use F a k e F o ofor the default behavior, as duplicating it in the mock object is, well, a lot of work. When you define the mock class using Google Mock, you can have it delegate its default action to a fake class you already have, using this pattern: u s i n g : : t e s t i n g : : _ ; u s i n g : : t e s t i n g : : I n v o k e ; c l a s s M o c k F o o : p u b l i c F o o { p u b l i c : / / N o r m a l m o c k m e t h o d d e f i n i t i o n s u s i n g G o o g l e M o c k . M O C K _ M E T H O D 1 ( D o T h i s , c h a r ( i n t n ) ) ; M O C K _ M E T H O D 2 ( D o T h a t , v o i d ( c o n s t c h a r * s , i n t * p ) ) ; / / D e l e g a t e s t h e d e f a u l t a c t i o n s o f t h e m e t h o d s t o a F a k e F o o o b j e c t . / / T h i s m u s t b e c a l l e d * b e f o r e * t h e c u s t o m O N _ C A L L ( ) s t a t e m e n t s . v o i d D e l e g a t e T o F a k e ( ) { O N _ C A L L ( * t h i s , D o T h i s ( _ ) ) . W i l l B y D e f a u l t ( I n v o k e ( & f a k e _ , & F a k e F o o : : D o T h i s ) ) ; O N _ C A L L ( * t h i s , D o T h a t ( _ , _ ) ) . W i l l B y D e f a u l t ( I n v o k e ( & f a k e _ , & F a k e F o o : : D o T h a t ) ) ; } p r i v a t e : F a k e F o o f a k e _ ; / / K e e p s a n i n s t a n c e o f t h e f a k e i n t h e m o c k . } ; With that, you can use M o c k F o oin your tests as usual. Just remember that if you don't explicitly set an action in an O N _ C A L L ( )or E X P E C T _ C A L L ( ) , the fake will be called upon to do it: u s i n g : : t e s t i n g : : _ ; T E S T ( A b c T e s t , X y z ) { M o c k F o o f o o ; f o o . D e l e g a t e T o F a k e ( ) ; / / E n a b l e s t h e f a k e f o r d e l e g a t i o n . / / P u t y o u r O N _ C A L L ( f o o , . . . ) s h e r e , i f a n y .
  • 8. 8/23/13 CookBook - googlemock - Google C++ Mocking Framework Cookbook - Google C++ Mocking Framework - Google Project Hosting https://code.google.com/p/googlemock/wiki/CookBook 8/41 / / N o a c t i o n s p e c i f i e d , m e a n i n g t o u s e t h e d e f a u l t a c t i o n . E X P E C T _ C A L L ( f o o , D o T h i s ( 5 ) ) ; E X P E C T _ C A L L ( f o o , D o T h a t ( _ , _ ) ) ; i n t n = 0 ; E X P E C T _ E Q ( ' + ' , f o o . D o T h i s ( 5 ) ) ; / / F a k e F o o : : D o T h i s ( ) i s i n v o k e d . f o o . D o T h a t ( " H i " , & n ) ; / / F a k e F o o : : D o T h a t ( ) i s i n v o k e d . E X P E C T _ E Q ( 2 , n ) ; } Some tips: If you want, you can still override the default action by providing your own O N _ C A L L ( )or using . W i l l O n c e ( )/ . W i l l R e p e a t e d l y ( )in E X P E C T _ C A L L ( ) . In D e l e g a t e T o F a k e ( ) , you only need to delegate the methods whose fake implementation you intend to use. The general technique discussed here works for overloaded methods, but you'll need to tell the compiler which version you mean. To disambiguate a mock function (the one you specify inside the parentheses of O N _ C A L L ( ) ), see the "Selecting Between Overloaded Functions" section on this page; to disambiguate a fake function (the one you place inside I n v o k e ( ) ), use a s t a t i c _ c a s tto specify the function's type. For instance, if class F o ohas methods c h a r D o T h i s ( i n t n )and b o o l D o T h i s ( d o u b l e x ) c o n s t , and you want to invoke the latter, you need to write I n v o k e ( & f a k e _ , s t a t i c _ c a s t < b o o l ( F a k e F o o : : * ) ( d o u b l e ) c o n s t > ( & F a k e F o o : : D o T h i s ) )instead of I n v o k e ( & f a k e _ , & F a k e F o o : : D o T h i s )(The strange­looking thing inside the angled brackets of s t a t i c _ c a s tis the type of a function pointer to the second D o T h i s ( )method.). Having to mix a mock and a fake is often a sign of something gone wrong. Perhaps you haven't got used to the interaction­based way of testing yet. Or perhaps your interface is taking on too many roles and should be split up. Therefore, don't abuse this. We would only recommend to do it as an intermediate step when you are refactoring your code. Regarding the tip on mixing a mock and a fake, here's an example on why it may be a bad sign: Suppose you have a class S y s t e mfor low­level system operations. In particular, it does file and I/O operations. And suppose you want to test how your code uses S y s t e mto do I/O, and you just want the file operations to work normally. If you mock out the entire S y s t e mclass, you'll have to provide a fake implementation for the file operation part, which suggests that S y s t e mis taking on too many roles. Instead, you can define a F i l e O p sinterface and an I O O p sinterface and split S y s t e m 's functionalities into the two. Then you can mock I O O p s without mocking F i l e O p s . Delegating Calls to a Real Object When using testing doubles (mocks, fakes, stubs, and etc), sometimes their behaviors will differ from those of the real objects. This difference could be either intentional (as in simulating an error such that you can test the error handling code) or unintentional. If your mocks have different behaviors than the real objects by mistake, you could end up with code that passes the tests but fails in production. You can use the delegating­to­real technique to ensure that your mock has the same behavior as the real object while retaining the ability to validate calls. This technique is very similar to the delegating­to­fake technique, the difference being that we use a real object instead of a fake. Here's an example: u s i n g : : t e s t i n g : : _ ; u s i n g : : t e s t i n g : : A t L e a s t ; u s i n g : : t e s t i n g : : I n v o k e ; c l a s s M o c k F o o : p u b l i c F o o { p u b l i c : M o c k F o o ( ) { / / B y d e f a u l t , a l l c a l l s a r e d e l e g a t e d t o t h e r e a l o b j e c t . O N _ C A L L ( * t h i s , D o T h i s ( ) ) . W i l l B y D e f a u l t ( I n v o k e ( & r e a l _ , & F o o : : D o T h i s ) ) ; O N _ C A L L ( * t h i s , D o T h a t ( _ ) ) . W i l l B y D e f a u l t ( I n v o k e ( & r e a l _ , & F o o : : D o T h a t ) ) ; . . . } M O C K _ M E T H O D 0 ( D o T h i s , . . . ) ; M O C K _ M E T H O D 1 ( D o T h a t , . . . ) ; . . . p r i v a t e : F o o r e a l _ ; } ; . . . M o c k F o o m o c k ; E X P E C T _ C A L L ( m o c k , D o T h i s ( ) ) . T i m e s ( 3 ) ; E X P E C T _ C A L L ( m o c k , D o T h a t ( " H i " ) )
  • 9. 8/23/13 CookBook - googlemock - Google C++ Mocking Framework Cookbook - Google C++ Mocking Framework - Google Project Hosting https://code.google.com/p/googlemock/wiki/CookBook 9/41 E X P E C T _ C A L L ( m o c k , D o T h a t ( " H i " ) ) . T i m e s ( A t L e a s t ( 1 ) ) ; . . . u s e m o c k i n t e s t . . . With this, Google Mock will verify that your code made the right calls (with the right arguments, in the right order, called the right number of times, etc), and a real object will answer the calls (so the behavior will be the same as in production). This gives you the best of both worlds. Delegating Calls to a Parent Class Ideally, you should code to interfaces, whose methods are all pure virtual. In reality, sometimes you do need to mock a virtual method that is not pure (i.e, it already has an implementation). For example: c l a s s F o o { p u b l i c : v i r t u a l ~ F o o ( ) ; v i r t u a l v o i d P u r e ( i n t n ) = 0 ; v i r t u a l i n t C o n c r e t e ( c o n s t c h a r * s t r ) { . . . } } ; c l a s s M o c k F o o : p u b l i c F o o { p u b l i c : / / M o c k i n g a p u r e m e t h o d . M O C K _ M E T H O D 1 ( P u r e , v o i d ( i n t n ) ) ; / / M o c k i n g a c o n c r e t e m e t h o d . F o o : : C o n c r e t e ( ) i s s h a d o w e d . M O C K _ M E T H O D 1 ( C o n c r e t e , i n t ( c o n s t c h a r * s t r ) ) ; } ; Sometimes you may want to call F o o : : C o n c r e t e ( )instead of M o c k F o o : : C o n c r e t e ( ) . Perhaps you want to do it as part of a stub action, or perhaps your test doesn't need to mock C o n c r e t e ( )at all (but it would be oh­so painful to have to define a new mock class whenever you don't need to mock one of its methods). The trick is to leave a back door in your mock class for accessing the real methods in the base class: c l a s s M o c k F o o : p u b l i c F o o { p u b l i c : / / M o c k i n g a p u r e m e t h o d . M O C K _ M E T H O D 1 ( P u r e , v o i d ( i n t n ) ) ; / / M o c k i n g a c o n c r e t e m e t h o d . F o o : : C o n c r e t e ( ) i s s h a d o w e d . M O C K _ M E T H O D 1 ( C o n c r e t e , i n t ( c o n s t c h a r * s t r ) ) ; / / U s e t h i s t o c a l l C o n c r e t e ( ) d e f i n e d i n F o o . i n t F o o C o n c r e t e ( c o n s t c h a r * s t r ) { r e t u r n F o o : : C o n c r e t e ( s t r ) ; } } ; Now, you can call F o o : : C o n c r e t e ( )inside an action by: u s i n g : : t e s t i n g : : _ ; u s i n g : : t e s t i n g : : I n v o k e ; . . . E X P E C T _ C A L L ( f o o , C o n c r e t e ( _ ) ) . W i l l O n c e ( I n v o k e ( & f o o , & M o c k F o o : : F o o C o n c r e t e ) ) ; or tell the mock object that you don't want to mock C o n c r e t e ( ) : u s i n g : : t e s t i n g : : I n v o k e ; . . . O N _ C A L L ( f o o , C o n c r e t e ( _ ) ) . W i l l B y D e f a u l t ( I n v o k e ( & f o o , & M o c k F o o : : F o o C o n c r e t e ) ) ; (Why don't we just write I n v o k e ( & f o o , & F o o : : C o n c r e t e ) ? If you do that, M o c k F o o : : C o n c r e t e ( )will be called (and cause an infinite recursion) since F o o : : C o n c r e t e ( )is virtual. That's just how C++ works.) Using Matchers Matching Argument Values Exactly You can specify exactly which arguments a mock method is expecting: u s i n g : : t e s t i n g : : R e t u r n ; . . . E X P E C T _ C A L L ( f o o , D o T h i s ( 5 ) )
  • 10. 8/23/13 CookBook - googlemock - Google C++ Mocking Framework Cookbook - Google C++ Mocking Framework - Google Project Hosting https://code.google.com/p/googlemock/wiki/CookBook 10/41 . W i l l O n c e ( R e t u r n ( ' a ' ) ) ; E X P E C T _ C A L L ( f o o , D o T h a t ( " H e l l o " , b a r ) ) ; Using Simple Matchers You can use matchers to match arguments that have a certain property: u s i n g : : t e s t i n g : : G e ; u s i n g : : t e s t i n g : : N o t N u l l ; u s i n g : : t e s t i n g : : R e t u r n ; . . . E X P E C T _ C A L L ( f o o , D o T h i s ( G e ( 5 ) ) ) / / T h e a r g u m e n t m u s t b e > = 5 . . W i l l O n c e ( R e t u r n ( ' a ' ) ) ; E X P E C T _ C A L L ( f o o , D o T h a t ( " H e l l o " , N o t N u l l ( ) ) ) ; / / T h e s e c o n d a r g u m e n t m u s t n o t b e N U L L . A frequently used matcher is _ , which matches anything: u s i n g : : t e s t i n g : : _ ; u s i n g : : t e s t i n g : : N o t N u l l ; . . . E X P E C T _ C A L L ( f o o , D o T h a t ( _ , N o t N u l l ( ) ) ) ; Combining Matchers You can build complex matchers from existing ones using A l l O f ( ) , A n y O f ( ) , and N o t ( ) : u s i n g : : t e s t i n g : : A l l O f ; u s i n g : : t e s t i n g : : G t ; u s i n g : : t e s t i n g : : H a s S u b s t r ; u s i n g : : t e s t i n g : : N e ; u s i n g : : t e s t i n g : : N o t ; . . . / / T h e a r g u m e n t m u s t b e > 5 a n d ! = 1 0 . E X P E C T _ C A L L ( f o o , D o T h i s ( A l l O f ( G t ( 5 ) , N e ( 1 0 ) ) ) ) ; / / T h e f i r s t a r g u m e n t m u s t n o t c o n t a i n s u b - s t r i n g " b l a h " . E X P E C T _ C A L L ( f o o , D o T h a t ( N o t ( H a s S u b s t r ( " b l a h " ) ) , N U L L ) ) ; Casting Matchers Google Mock matchers are statically typed, meaning that the compiler can catch your mistake if you use a matcher of the wrong type (for example, if you use E q ( 5 )to match a s t r i n gargument). Good for you! Sometimes, however, you know what you're doing and want the compiler to give you some slack. One example is that you have a matcher for l o n gand the argument you want to match is i n t . While the two types aren't exactly the same, there is nothing really wrong with using a M a t c h e r < l o n g >to match an i n t­ after all, we can first convert the i n targument to a l o n gbefore giving it to the matcher. To support this need, Google Mock gives you the S a f e M a t c h e r C a s t < T > ( m )function. It casts a matcher mto type M a t c h e r < T > . To ensure safety, Google Mock checks that (let Ube the type maccepts): 1. Type Tcan be implicitly cast to type U ; 2. When both Tand Uare built­in arithmetic types (b o o l , integers, and floating­point numbers), the conversion from Tto Uis not lossy (in other words, any value representable by Tcan also be represented by U ); and 3. When Uis a reference, Tmust also be a reference (as the underlying matcher may be interested in the address of the Uvalue). The code won't compile if any of these conditions isn't met. Here's one example: u s i n g : : t e s t i n g : : S a f e M a t c h e r C a s t ; / / A b a s e c l a s s a n d a c h i l d c l a s s . c l a s s B a s e { . . . } ; c l a s s D e r i v e d : p u b l i c B a s e { . . . } ; c l a s s M o c k F o o : p u b l i c F o o { p u b l i c : M O C K _ M E T H O D 1 ( D o T h i s , v o i d ( D e r i v e d * d e r i v e d ) ) ;
  • 11. 8/23/13 CookBook - googlemock - Google C++ Mocking Framework Cookbook - Google C++ Mocking Framework - Google Project Hosting https://code.google.com/p/googlemock/wiki/CookBook 11/41 } ; . . . M o c k F o o f o o ; / / m i s a M a t c h e r < B a s e * > w e g o t f r o m s o m e w h e r e . E X P E C T _ C A L L ( f o o , D o T h i s ( S a f e M a t c h e r C a s t < D e r i v e d * > ( m ) ) ) ; If you find S a f e M a t c h e r C a s t < T > ( m )too limiting, you can use a similar function M a t c h e r C a s t < T > ( m ) . The difference is that M a t c h e r C a s tworks as long as you can s t a t i c _ c a s ttype Tto type U . M a t c h e r C a s tessentially lets you bypass C++'s type system (s t a t i c _ c a s tisn't always safe as it could throw away information, for example), so be careful not to misuse/abuse it. Selecting Between Overloaded Functions If you expect an overloaded function to be called, the compiler may need some help on which overloaded version it is. To disambiguate functions overloaded on the const­ness of this object, use the C o n s t ( )argument wrapper. u s i n g : : t e s t i n g : : R e t u r n R e f ; c l a s s M o c k F o o : p u b l i c F o o { . . . M O C K _ M E T H O D 0 ( G e t B a r , B a r & ( ) ) ; M O C K _ C O N S T _ M E T H O D 0 ( G e t B a r , c o n s t B a r & ( ) ) ; } ; . . . M o c k F o o f o o ; B a r b a r 1 , b a r 2 ; E X P E C T _ C A L L ( f o o , G e t B a r ( ) ) / / T h e n o n - c o n s t G e t B a r ( ) . . W i l l O n c e ( R e t u r n R e f ( b a r 1 ) ) ; E X P E C T _ C A L L ( C o n s t ( f o o ) , G e t B a r ( ) ) / / T h e c o n s t G e t B a r ( ) . . W i l l O n c e ( R e t u r n R e f ( b a r 2 ) ) ; (C o n s t ( )is defined by Google Mock and returns a c o n s treference to its argument.) To disambiguate overloaded functions with the same number of arguments but different argument types, you may need to specify the exact type of a matcher, either by wrapping your matcher in M a t c h e r < t y p e > ( ) , or using a matcher whose type is fixed (T y p e d E q < t y p e > , A n < t y p e > ( ) , etc): u s i n g : : t e s t i n g : : A n ; u s i n g : : t e s t i n g : : L t ; u s i n g : : t e s t i n g : : M a t c h e r ; u s i n g : : t e s t i n g : : T y p e d E q ; c l a s s M o c k P r i n t e r : p u b l i c P r i n t e r { p u b l i c : M O C K _ M E T H O D 1 ( P r i n t , v o i d ( i n t n ) ) ; M O C K _ M E T H O D 1 ( P r i n t , v o i d ( c h a r c ) ) ; } ; T E S T ( P r i n t e r T e s t , P r i n t ) { M o c k P r i n t e r p r i n t e r ; E X P E C T _ C A L L ( p r i n t e r , P r i n t ( A n < i n t > ( ) ) ) ; / / v o i d P r i n t ( i n t ) ; E X P E C T _ C A L L ( p r i n t e r , P r i n t ( M a t c h e r < i n t > ( L t ( 5 ) ) ) ) ; / / v o i d P r i n t ( i n t ) ; E X P E C T _ C A L L ( p r i n t e r , P r i n t ( T y p e d E q < c h a r > ( ' a ' ) ) ) ; / / v o i d P r i n t ( c h a r ) ; p r i n t e r . P r i n t ( 3 ) ; p r i n t e r . P r i n t ( 6 ) ; p r i n t e r . P r i n t ( ' a ' ) ; } Performing Different Actions Based on the Arguments When a mock method is called, the last matching expectation that's still active will be selected (think "newer overrides older"). So, you can make a method do different things depending on its argument values like this: u s i n g : : t e s t i n g : : _ ; u s i n g : : t e s t i n g : : L t ; u s i n g : : t e s t i n g : : R e t u r n ; . . . / / T h e d e f a u l t c a s e .
  • 12. 8/23/13 CookBook - googlemock - Google C++ Mocking Framework Cookbook - Google C++ Mocking Framework - Google Project Hosting https://code.google.com/p/googlemock/wiki/CookBook 12/41 / / T h e d e f a u l t c a s e . E X P E C T _ C A L L ( f o o , D o T h i s ( _ ) ) . W i l l R e p e a t e d l y ( R e t u r n ( ' b ' ) ) ; / / T h e m o r e s p e c i f i c c a s e . E X P E C T _ C A L L ( f o o , D o T h i s ( L t ( 5 ) ) ) . W i l l R e p e a t e d l y ( R e t u r n ( ' a ' ) ) ; Now, if f o o . D o T h i s ( )is called with a value less than 5, ' a 'will be returned; otherwise ' b 'will be returned. Matching Multiple Arguments as a Whole Sometimes it's not enough to match the arguments individually. For example, we may want to say that the first argument must be less than the second argument. The W i t h ( )clause allows us to match all arguments of a mock function as a whole. For example, u s i n g : : t e s t i n g : : _ ; u s i n g : : t e s t i n g : : L t ; u s i n g : : t e s t i n g : : N e ; . . . E X P E C T _ C A L L ( f o o , I n R a n g e ( N e ( 0 ) , _ ) ) . W i t h ( L t ( ) ) ; says that the first argument of I n R a n g e ( )must not be 0, and must be less than the second argument. The expression inside W i t h ( )must be a matcher of type M a t c h e r < t r 1 : : t u p l e < A 1 , . . . , A n > > , where A 1 , ..., A nare the types of the function arguments. You can also write A l l A r g s ( m )instead of minside . W i t h ( ) . The two forms are equivalent, but . W i t h ( A l l A r g s ( L t ( ) ) )is more readable than . W i t h ( L t ( ) ) . You can use A r g s < k 1 , . . . , k n > ( m )to match the nselected arguments (as a tuple) against m . For example, u s i n g : : t e s t i n g : : _ ; u s i n g : : t e s t i n g : : A l l O f ; u s i n g : : t e s t i n g : : A r g s ; u s i n g : : t e s t i n g : : L t ; . . . E X P E C T _ C A L L ( f o o , B l a h ( _ , _ , _ ) ) . W i t h ( A l l O f ( A r g s < 0 , 1 > ( L t ( ) ) , A r g s < 1 , 2 > ( L t ( ) ) ) ) ; says that B l a h ( )will be called with arguments x , y , and zwhere x < y < z . As a convenience and example, Google Mock provides some matchers for 2­tuples, including the L t ( )matcher above. See the CheatSheet for the complete list. Note that if you want to pass the arguments to a predicate of your own (e.g. . W i t h ( A r g s < 0 , 1 > ( T r u l y ( & M y P r e d i c a t e ) ) ) ), that predicate MUST be written to take a t r 1 : : t u p l eas its argument; Google Mock will pass the nselected arguments as one single tuple to the predicate. Using Matchers as Predicates Have you noticed that a matcher is just a fancy predicate that also knows how to describe itself? Many existing algorithms take predicates as arguments (e.g. those defined in STL's < a l g o r i t h m >header), and it would be a shame if Google Mock matchers are not allowed to participate. Luckily, you can use a matcher where a unary predicate functor is expected by wrapping it inside the M a t c h e s ( )function. For example, # i n c l u d e < a l g o r i t h m > # i n c l u d e < v e c t o r > s t d : : v e c t o r < i n t > v ; . . . / / H o w m a n y e l e m e n t s i n v a r e > = 1 0 ? c o n s t i n t c o u n t = c o u n t _ i f ( v . b e g i n ( ) , v . e n d ( ) , M a t c h e s ( G e ( 1 0 ) ) ) ; Since you can build complex matchers from simpler ones easily using Google Mock, this gives you a way to conveniently construct composite predicates (doing the same using STL's < f u n c t i o n a l >header is just painful). For example, here's a predicate that's satisfied by any number that is >= 0, <= 100, and != 50: M a t c h e s ( A l l O f ( G e ( 0 ) , L e ( 1 0 0 ) , N e ( 5 0 ) ) ) Using Matchers in Google Test Assertions Since matchers are basically predicates that also know how to describe themselves, there is a way to take advantage of them in Google Test assertions. It's called A S S E R T _ T H A Tand E X P E C T _ T H A T :
  • 13. 8/23/13 CookBook - googlemock - Google C++ Mocking Framework Cookbook - Google C++ Mocking Framework - Google Project Hosting https://code.google.com/p/googlemock/wiki/CookBook 13/41 A S S E R T _ T H A T ( v a l u e , m a t c h e r ) ; / / A s s e r t s t h a t v a l u e m a t c h e s m a t c h e r . E X P E C T _ T H A T ( v a l u e , m a t c h e r ) ; / / T h e n o n - f a t a l v e r s i o n . For example, in a Google Test test you can write: # i n c l u d e " g m o c k / g m o c k . h " u s i n g : : t e s t i n g : : A l l O f ; u s i n g : : t e s t i n g : : G e ; u s i n g : : t e s t i n g : : L e ; u s i n g : : t e s t i n g : : M a t c h e s R e g e x ; u s i n g : : t e s t i n g : : S t a r t s W i t h ; . . . E X P E C T _ T H A T ( F o o ( ) , S t a r t s W i t h ( " H e l l o " ) ) ; E X P E C T _ T H A T ( B a r ( ) , M a t c h e s R e g e x ( " L i n e d + " ) ) ; A S S E R T _ T H A T ( B a z ( ) , A l l O f ( G e ( 5 ) , L e ( 1 0 ) ) ) ; which (as you can probably guess) executes F o o ( ) , B a r ( ) , and B a z ( ) , and verifies that: F o o ( )returns a string that starts with " H e l l o " . B a r ( )returns a string that matches regular expression " L i n e d + " . B a z ( )returns a number in the range [5, 10]. The nice thing about these macros is that they read like English. They generate informative messages too. For example, if the first E X P E C T _ T H A T ( )above fails, the message will be something like: V a l u e o f : F o o ( ) A c t u a l : " H i , w o r l d ! " E x p e c t e d : s t a r t s w i t h " H e l l o " Credit: The idea of ( A S S E R T | E X P E C T ) _ T H A Twas stolen from the Hamcrest project, which adds a s s e r t T h a t ( )to JUnit. Using Predicates as Matchers Google Mock provides a built­in set of matchers. In case you find them lacking, you can use an arbitray unary predicate function or functor as a matcher ­ as long as the predicate accepts a value of the type you want. You do this by wrapping the predicate inside the T r u l y ( )function, for example: u s i n g : : t e s t i n g : : T r u l y ; i n t I s E v e n ( i n t n ) { r e t u r n ( n % 2 ) = = 0 ? 1 : 0 ; } . . . / / B a r ( ) m u s t b e c a l l e d w i t h a n e v e n n u m b e r . E X P E C T _ C A L L ( f o o , B a r ( T r u l y ( I s E v e n ) ) ) ; Note that the predicate function / functor doesn't have to return b o o l . It works as long as the return value can be used as the condition in statement i f ( c o n d i t i o n ) . . . . Matching Arguments that Are Not Copyable When you do an E X P E C T _ C A L L ( m o c k _ o b j , F o o ( b a r ) ) , Google Mock saves away a copy of b a r . When F o o ( )is called later, Google Mock compares the argument to F o o ( )with the saved copy of b a r . This way, you don't need to worry about b a rbeing modified or destroyed after the E X P E C T _ C A L L ( )is executed. The same is true when you use matchers like E q ( b a r ) , L e ( b a r ) , and so on. But what if b a rcannot be copied (i.e. has no copy constructor)? You could define your own matcher function and use it with T r u l y ( ) , as the previous couple of recipes have shown. Or, you may be able to get away from it if you can guarantee that b a rwon't be changed after the E X P E C T _ C A L L ( )is executed. Just tell Google Mock that it should save a reference to b a r , instead of a copy of it. Here's how: u s i n g : : t e s t i n g : : E q ; u s i n g : : t e s t i n g : : B y R e f ; u s i n g : : t e s t i n g : : L t ; . . . / / E x p e c t s t h a t F o o ( ) ' s a r g u m e n t = = b a r . E X P E C T _ C A L L ( m o c k _ o b j , F o o ( E q ( B y R e f ( b a r ) ) ) ) ; / / E x p e c t s t h a t F o o ( ) ' s a r g u m e n t < b a r . E X P E C T _ C A L L ( m o c k _ o b j , F o o ( L t ( B y R e f ( b a r ) ) ) ) ;
  • 14. 8/23/13 CookBook - googlemock - Google C++ Mocking Framework Cookbook - Google C++ Mocking Framework - Google Project Hosting https://code.google.com/p/googlemock/wiki/CookBook 14/41 Remember: if you do this, don't change b a rafter the E X P E C T _ C A L L ( ) , or the result is undefined. Validating a Member of an Object Often a mock function takes a reference to object as an argument. When matching the argument, you may not want to compare the entire object against a fixed object, as that may be over­specification. Instead, you may need to validate a certain member variable or the result of a certain getter method of the object. You can do this with F i e l d ( )and P r o p e r t y ( ) . More specifically, F i e l d ( & F o o : : b a r , m ) is a matcher that matches a F o oobject whose b a rmember variable satisfies matcher m . P r o p e r t y ( & F o o : : b a z , m ) is a matcher that matches a F o oobject whose b a z ( )method returns a value that satisfies matcher m . For example: F i e l d ( & F o o : : n u m b e r , G e ( 3 ) ) Matches xwhere x . n u m b e r > = 3 . P r o p e r t y ( & F o o : : n a m e , S t a r t s W i t h ( " J o h n " ) ) Matches xwhere x . n a m e ( )starts with " J o h n " . Note that in P r o p e r t y ( & F o o : : b a z , . . . ) , method b a z ( )must take no argument and be declared as c o n s t . BTW, F i e l d ( )and P r o p e r t y ( )can also match plain pointers to objects. For instance, F i e l d ( & F o o : : n u m b e r , G e ( 3 ) ) matches a plain pointer pwhere p - > n u m b e r > = 3 . If pis N U L L , the match will always fail regardless of the inner matcher. What if you want to validate more than one members at the same time? Remember that there is A l l O f ( ) . Validating the Value Pointed to by a Pointer Argument C++ functions often take pointers as arguments. You can use matchers like I s N u l l ( ) , N o t N u l l ( ) , and other comparison matchers to match a pointer, but what if you want to make sure the value pointed to by the pointer, instead of the pointer itself, has a certain property? Well, you can use the P o i n t e e ( m )matcher. P o i n t e e ( m )matches a pointer iff mmatches the value the pointer points to. For example: u s i n g : : t e s t i n g : : G e ; u s i n g : : t e s t i n g : : P o i n t e e ; . . . E X P E C T _ C A L L ( f o o , B a r ( P o i n t e e ( G e ( 3 ) ) ) ) ; expects f o o . B a r ( )to be called with a pointer that points to a value greater than or equal to 3. One nice thing about P o i n t e e ( )is that it treats a N U L Lpointer as a match failure, so you can write P o i n t e e ( m )instead of A l l O f ( N o t N u l l ( ) , P o i n t e e ( m ) ) without worrying that a N U L Lpointer will crash your test. Also, did we tell you that P o i n t e e ( )works with both raw pointers and smart pointers (l i n k e d _ p t r , s h a r e d _ p t r , s c o p e d _ p t r , and etc)? What if you have a pointer to pointer? You guessed it ­ you can use nested P o i n t e e ( )to probe deeper inside the value. For example, P o i n t e e ( P o i n t e e ( L t ( 3 ) ) )matches a pointer that points to a pointer that points to a number less than 3 (what a mouthful...). Testing a Certain Property of an Object Sometimes you want to specify that an object argument has a certain property, but there is no existing matcher that does this. If you want good error messages, you should define a matcher. If you want to do it quick and dirty, you could get away with writing an ordinary function. Let's say you have a mock function that takes an object of type F o o , which has an i n t b a r ( )method and an i n t b a z ( )method, and you want to constrain that the argument's b a r ( )value plus its b a z ( )value is a given number. Here's how you can define a matcher to do it: u s i n g : : t e s t i n g : : M a t c h e r I n t e r f a c e ; u s i n g : : t e s t i n g : : M a t c h R e s u l t L i s t e n e r ; c l a s s B a r P l u s B a z E q M a t c h e r : p u b l i c M a t c h e r I n t e r f a c e < c o n s t F o o & > { p u b l i c :
  • 15. 8/23/13 CookBook - googlemock - Google C++ Mocking Framework Cookbook - Google C++ Mocking Framework - Google Project Hosting https://code.google.com/p/googlemock/wiki/CookBook 15/41 p u b l i c : e x p l i c i t B a r P l u s B a z E q M a t c h e r ( i n t e x p e c t e d _ s u m ) : e x p e c t e d _ s u m _ ( e x p e c t e d _ s u m ) { } v i r t u a l b o o l M a t c h A n d E x p l a i n ( c o n s t F o o & f o o , M a t c h R e s u l t L i s t e n e r * l i s t e n e r ) c o n s t { r e t u r n ( f o o . b a r ( ) + f o o . b a z ( ) ) = = e x p e c t e d _ s u m _ ; } v i r t u a l v o i d D e s c r i b e T o ( : : s t d : : o s t r e a m * o s ) c o n s t { * o s < < " b a r ( ) + b a z ( ) e q u a l s " < < e x p e c t e d _ s u m _ ; } v i r t u a l v o i d D e s c r i b e N e g a t i o n T o ( : : s t d : : o s t r e a m * o s ) c o n s t { * o s < < " b a r ( ) + b a z ( ) d o e s n o t e q u a l " < < e x p e c t e d _ s u m _ ; } p r i v a t e : c o n s t i n t e x p e c t e d _ s u m _ ; } ; i n l i n e M a t c h e r < c o n s t F o o & > B a r P l u s B a z E q ( i n t e x p e c t e d _ s u m ) { r e t u r n M a k e M a t c h e r ( n e w B a r P l u s B a z E q M a t c h e r ( e x p e c t e d _ s u m ) ) ; } . . . E X P E C T _ C A L L ( . . . , D o T h i s ( B a r P l u s B a z E q ( 5 ) ) ) . . . ; Matching Containers Sometimes an STL container (e.g. list, vector, map, ...) is passed to a mock function and you may want to validate it. Since most STL containers support the = =operator, you can write E q ( e x p e c t e d _ c o n t a i n e r )or simply e x p e c t e d _ c o n t a i n e rto match a container exactly. Sometimes, though, you may want to be more flexible (for example, the first element must be an exact match, but the second element can be any positive number, and so on). Also, containers used in tests often have a small number of elements, and having to define the expected container out­of­line is a bit of a hassle. You can use the E l e m e n t s A r e ( )or U n o r d e r e d E l e m e n t s A r e ( )matcher in such cases: u s i n g : : t e s t i n g : : _ ; u s i n g : : t e s t i n g : : E l e m e n t s A r e ; u s i n g : : t e s t i n g : : G t ; . . . M O C K _ M E T H O D 1 ( F o o , v o i d ( c o n s t v e c t o r < i n t > & n u m b e r s ) ) ; . . . E X P E C T _ C A L L ( m o c k , F o o ( E l e m e n t s A r e ( 1 , G t ( 0 ) , _ , 5 ) ) ) ; The above matcher says that the container must have 4 elements, which must be 1, greater than 0, anything, and 5 respectively. If you instead write: u s i n g : : t e s t i n g : : _ ; u s i n g : : t e s t i n g : : G t ; u s i n g : : t e s t i n g : : U n o r d e r e d E l e m e n t s A r e ; . . . M O C K _ M E T H O D 1 ( F o o , v o i d ( c o n s t v e c t o r < i n t > & n u m b e r s ) ) ; . . . E X P E C T _ C A L L ( m o c k , F o o ( U n o r d e r e d E l e m e n t s A r e ( 1 , G t ( 0 ) , _ , 5 ) ) ) ; It means that the container must have 4 elements, which under some permutation must be 1, greater than 0, anything, and 5 respectively. E l e m e n t s A r e ( )and U n o r d e r e d E l e m e n t s A r e ( )are overloaded to take 0 to 10 arguments. If more are needed, you can place them in a C­style array and use E l e m e n t s A r e A r r a y ( )or U n o r d e r e d E l e m e n t s A r e A r r a y ( )instead: u s i n g : : t e s t i n g : : E l e m e n t s A r e A r r a y ; . . . / / E l e m e n t s A r e A r r a y a c c e p t s a n a r r a y o f e l e m e n t v a l u e s . c o n s t i n t e x p e c t e d _ v e c t o r 1 [ ] = { 1 , 5 , 2 , 4 , . . . } ;
  • 16. 8/23/13 CookBook - googlemock - Google C++ Mocking Framework Cookbook - Google C++ Mocking Framework - Google Project Hosting https://code.google.com/p/googlemock/wiki/CookBook 16/41 c o n s t i n t e x p e c t e d _ v e c t o r 1 [ ] = { 1 , 5 , 2 , 4 , . . . } ; E X P E C T _ C A L L ( m o c k , F o o ( E l e m e n t s A r e A r r a y ( e x p e c t e d _ v e c t o r 1 ) ) ) ; / / O r , a n a r r a y o f e l e m e n t m a t c h e r s . M a t c h e r < i n t > e x p e c t e d _ v e c t o r 2 = { 1 , G t ( 2 ) , _ , 3 , . . . } ; E X P E C T _ C A L L ( m o c k , F o o ( E l e m e n t s A r e A r r a y ( e x p e c t e d _ v e c t o r 2 ) ) ) ; In case the array needs to be dynamically created (and therefore the array size cannot be inferred by the compiler), you can give E l e m e n t s A r e A r r a y ( )an additional argument to specify the array size: u s i n g : : t e s t i n g : : E l e m e n t s A r e A r r a y ; . . . i n t * c o n s t e x p e c t e d _ v e c t o r 3 = n e w i n t [ c o u n t ] ; . . . f i l l e x p e c t e d _ v e c t o r 3 w i t h v a l u e s . . . E X P E C T _ C A L L ( m o c k , F o o ( E l e m e n t s A r e A r r a y ( e x p e c t e d _ v e c t o r 3 , c o u n t ) ) ) ; Tips: E l e m e n t A r e * ( )works with any container that implements the STL iterator concept (i.e. it has a c o n s t _ i t e r a t o rtype and supports b e g i n ( ) / e n d ( ) ) and supports s i z e ( ) , not just the ones defined in STL. It will even work with container types yet to be written ­ as long as they follows the above pattern. You can use nested E l e m e n t A r e * ( )to match nested (multi­dimensional) containers. If the container is passed by pointer instead of by reference, just write P o i n t e e ( E l e m e n t s A r e * ( . . . ) ) . The order of elements matters for E l e m e n t s A r e * ( ) . Therefore don't use it with containers whose element order is undefined (e.g. h a s h _ m a p ). Sharing Matchers Under the hood, a Google Mock matcher object consists of a pointer to a ref­counted implementation object. Copying matchers is allowed and very efficient, as only the pointer is copied. When the last matcher that references the implementation object dies, the implementation object will be deleted. Therefore, if you have some complex matcher that you want to use again and again, there is no need to build it everytime. Just assign it to a matcher variable and use that variable repeatedly! For example, M a t c h e r < i n t > i n _ r a n g e = A l l O f ( G t ( 5 ) , L e ( 1 0 ) ) ; . . . u s e i n _ r a n g e a s a m a t c h e r i n m u l t i p l e E X P E C T _ C A L L s . . . Setting Expectations Knowing When to Expect O N _ C A L Lis likely the single most under­utilized construct in Google Mock. There are basically two constructs for defining the behavior of a mock object: O N _ C A L Land E X P E C T _ C A L L . The difference? O N _ C A L Ldefines what happens when a mock method is called, but doesn't imply any expectation on the method being called. E X P E C T _ C A L Lnot only defines the behavior, but also sets an expectation that the method will be called with the given arguments, for the given number of times (and in the given order when you specify the order too). Since E X P E C T _ C A L Ldoes more, isn't it better than O N _ C A L L ? Not really. Every E X P E C T _ C A L Ladds a constraint on the behavior of the code under test. Having more constraints than necessary is baaad ­ even worse than not having enough constraints. This may be counter­intuitive. How could tests that verify more be worse than tests that verify less? Isn't verification the whole point of tests? The answer, lies in what a test should verify. A good test verifies the contract of the code. If a test over­specifies, it doesn't leave enough freedom to the implementation. As a result, changing the implementation without breaking the contract (e.g. refactoring and optimization), which should be perfectly fine to do, can break such tests. Then you have to spend time fixing them, only to see them broken again the next time the implementation is changed. Keep in mind that one doesn't have to verify more than one property in one test. In fact, it's a good style to verify only one thing in one test. If you do that, a bug will likely break only one or two tests instead of dozens (which case would you rather debug?). If you are also in the habit of giving tests descriptive names that tell what they verify, you can often easily guess what's wrong just from the test log itself. So use O N _ C A L Lby default, and only use E X P E C T _ C A L Lwhen you actually intend to verify that the call is made. For example, you may have a bunch of O N _ C A L L s in your test fixture to set the common mock behavior shared by all tests in the same group, and write (scarcely) different E X P E C T _ C A L L s in different T E S T _ F s to verify different aspects of the code's behavior. Compared with the style where each T E S Thas many E X P E C T _ C A L L s, this leads to tests that are more resilient to implementational changes (and thus less likely to require maintenance) and makes the intent of the tests more obvious (so they are easier to maintain when you do need to maintain them). Ignoring Uninteresting Calls If you are not interested in how a mock method is called, just don't say anything about it. In this case, if the method is ever called, Google Mock will perform its default action to allow the test program to continue. If you are not happy with the default action taken by Google Mock, you can override it using D e f a u l t V a l u e < T > : : S e t ( )(described later in this document) or O N _ C A L L ( ) .
  • 17. 8/23/13 CookBook - googlemock - Google C++ Mocking Framework Cookbook - Google C++ Mocking Framework - Google Project Hosting https://code.google.com/p/googlemock/wiki/CookBook 17/41 override it using D e f a u l t V a l u e < T > : : S e t ( )(described later in this document) or O N _ C A L L ( ) . Please note that once you expressed interest in a particular mock method (via E X P E C T _ C A L L ( ) ), all invocations to it must match some expectation. If this function is called but the arguments don't match any E X P E C T _ C A L L ( )statement, it will be an error. Disallowing Unexpected Calls If a mock method shouldn't be called at all, explicitly say so: u s i n g : : t e s t i n g : : _ ; . . . E X P E C T _ C A L L ( f o o , B a r ( _ ) ) . T i m e s ( 0 ) ; If some calls to the method are allowed, but the rest are not, just list all the expected calls: u s i n g : : t e s t i n g : : A n y N u m b e r ; u s i n g : : t e s t i n g : : G t ; . . . E X P E C T _ C A L L ( f o o , B a r ( 5 ) ) ; E X P E C T _ C A L L ( f o o , B a r ( G t ( 1 0 ) ) ) . T i m e s ( A n y N u m b e r ( ) ) ; A call to f o o . B a r ( )that doesn't match any of the E X P E C T _ C A L L ( )statements will be an error. Expecting Ordered Calls Although an E X P E C T _ C A L L ( )statement defined earlier takes precedence when Google Mock tries to match a function call with an expectation, by default calls don't have to happen in the order E X P E C T _ C A L L ( )statements are written. For example, if the arguments match the matchers in the third E X P E C T _ C A L L ( ) , but not those in the first two, then the third expectation will be used. If you would rather have all calls occur in the order of the expectations, put the E X P E C T _ C A L L ( )statements in a block where you define a variable of type I n S e q u e n c e : u s i n g : : t e s t i n g : : _ ; u s i n g : : t e s t i n g : : I n S e q u e n c e ; { I n S e q u e n c e s ; E X P E C T _ C A L L ( f o o , D o T h i s ( 5 ) ) ; E X P E C T _ C A L L ( b a r , D o T h a t ( _ ) ) . T i m e s ( 2 ) ; E X P E C T _ C A L L ( f o o , D o T h i s ( 6 ) ) ; } In this example, we expect a call to f o o . D o T h i s ( 5 ) , followed by two calls to b a r . D o T h a t ( )where the argument can be anything, which are in turn followed by a call to f o o . D o T h i s ( 6 ) . If a call occurred out­of­order, Google Mock will report an error. Expecting Partially Ordered Calls Sometimes requiring everything to occur in a predetermined order can lead to brittle tests. For example, we may care about Aoccurring before both Band C , but aren't interested in the relative order of Band C . In this case, the test should reflect our real intent, instead of being overly constraining. Google Mock allows you to impose an arbitrary DAG (directed acyclic graph) on the calls. One way to express the DAG is to use the After clause of E X P E C T _ C A L L . Another way is via the I n S e q u e n c e ( )clause (not the same as the I n S e q u e n c eclass), which we borrowed from jMock 2. It's less flexible than A f t e r ( ) , but more convenient when you have long chains of sequential calls, as it doesn't require you to come up with different names for the expectations in the chains. Here's how it works: If we view E X P E C T _ C A L L ( )statements as nodes in a graph, and add an edge from node A to node B wherever A must occur before B, we can get a DAG. We use the term "sequence" to mean a directed path in this DAG. Now, if we decompose the DAG into sequences, we just need to know which sequences each E X P E C T _ C A L L ( )belongs to in order to be able to reconstruct the orginal DAG. So, to specify the partial order on the expectations we need to do two things: first to define some S e q u e n c eobjects, and then for each E X P E C T _ C A L L ( )say which S e q u e n c eobjects it is part of. Expectations in the same sequence must occur in the order they are written. For example, u s i n g : : t e s t i n g : : S e q u e n c e ; S e q u e n c e s 1 , s 2 ;
  • 18. 8/23/13 CookBook - googlemock - Google C++ Mocking Framework Cookbook - Google C++ Mocking Framework - Google Project Hosting https://code.google.com/p/googlemock/wiki/CookBook 18/41 S e q u e n c e s 1 , s 2 ; E X P E C T _ C A L L ( f o o , A ( ) ) . I n S e q u e n c e ( s 1 , s 2 ) ; E X P E C T _ C A L L ( b a r , B ( ) ) . I n S e q u e n c e ( s 1 ) ; E X P E C T _ C A L L ( b a r , C ( ) ) . I n S e q u e n c e ( s 2 ) ; E X P E C T _ C A L L ( f o o , D ( ) ) . I n S e q u e n c e ( s 2 ) ; specifies the following DAG (where s 1is A - > B , and s 2is `A ­> C ­> D`): + - - - > B | A - - - | | + - - - > C - - - > D This means that A must occur before B and C, and C must occur before D. There's no restriction about the order other than these. Controlling When an Expectation Retires When a mock method is called, Google Mock only consider expectations that are still active. An expectation is active when created, and becomes inactive (aka retires) when a call that has to occur later has occurred. For example, in u s i n g : : t e s t i n g : : _ ; u s i n g : : t e s t i n g : : S e q u e n c e ; S e q u e n c e s 1 , s 2 ; E X P E C T _ C A L L ( l o g , L o g ( W A R N I N G , _ , " F i l e t o o l a r g e . " ) ) / / # 1 . T i m e s ( A n y N u m b e r ( ) ) . I n S e q u e n c e ( s 1 , s 2 ) ; E X P E C T _ C A L L ( l o g , L o g ( W A R N I N G , _ , " D a t a s e t i s e m p t y . " ) ) / / # 2 . I n S e q u e n c e ( s 1 ) ; E X P E C T _ C A L L ( l o g , L o g ( W A R N I N G , _ , " U s e r n o t f o u n d . " ) ) / / # 3 . I n S e q u e n c e ( s 2 ) ; as soon as either #2 or #3 is matched, #1 will retire. If a warning " F i l e t o o l a r g e . "is logged after this, it will be an error. Note that an expectation doesn't retire automatically when it's saturated. For example, u s i n g : : t e s t i n g : : _ ; . . . E X P E C T _ C A L L ( l o g , L o g ( W A R N I N G , _ , _ ) ) ; / / # 1 E X P E C T _ C A L L ( l o g , L o g ( W A R N I N G , _ , " F i l e t o o l a r g e . " ) ) ; / / # 2 says that there will be exactly one warning with the message `"File too large."`. If the second warning contains this message too, #2 will match again and result in an upper­bound­violated error. If this is not what you want, you can ask an expectation to retire as soon as it becomes saturated: u s i n g : : t e s t i n g : : _ ; . . . E X P E C T _ C A L L ( l o g , L o g ( W A R N I N G , _ , _ ) ) ; / / # 1 E X P E C T _ C A L L ( l o g , L o g ( W A R N I N G , _ , " F i l e t o o l a r g e . " ) ) / / # 2 . R e t i r e s O n S a t u r a t i o n ( ) ; Here #2 can be used only once, so if you have two warnings with the message " F i l e t o o l a r g e . " , the first will match #2 and the second will match #1 ­ there will be no error. Using Actions Returning References from Mock Methods If a mock function's return type is a reference, you need to use R e t u r n R e f ( )instead of R e t u r n ( )to return a result: u s i n g : : t e s t i n g : : R e t u r n R e f ; c l a s s M o c k F o o : p u b l i c F o o {
  • 19. 8/23/13 CookBook - googlemock - Google C++ Mocking Framework Cookbook - Google C++ Mocking Framework - Google Project Hosting https://code.google.com/p/googlemock/wiki/CookBook 19/41 c l a s s M o c k F o o : p u b l i c F o o { p u b l i c : M O C K _ M E T H O D 0 ( G e t B a r , B a r & ( ) ) ; } ; . . . M o c k F o o f o o ; B a r b a r ; E X P E C T _ C A L L ( f o o , G e t B a r ( ) ) . W i l l O n c e ( R e t u r n R e f ( b a r ) ) ; Returning Live Values from Mock Methods The R e t u r n ( x )action saves a copy of xwhen the action is created, and always returns the same value whenever it's executed. Sometimes you may want to instead return the live value of x(i.e. its value at the time when the action is executed.). If the mock function's return type is a reference, you can do it using R e t u r n R e f ( x ) , as shown in the previous recipe ("Returning References from Mock Methods"). However, Google Mock doesn't let you use R e t u r n R e f ( )in a mock function whose return type is not a reference, as doing that usually indicates a user error. So, what shall you do? You may be tempted to try B y R e f ( ) : u s i n g t e s t i n g : : B y R e f ; u s i n g t e s t i n g : : R e t u r n ; c l a s s M o c k F o o : p u b l i c F o o { p u b l i c : M O C K _ M E T H O D 0 ( G e t V a l u e , i n t ( ) ) ; } ; . . . i n t x = 0 ; M o c k F o o f o o ; E X P E C T _ C A L L ( f o o , G e t V a l u e ( ) ) . W i l l R e p e a t e d l y ( R e t u r n ( B y R e f ( x ) ) ) ; x = 4 2 ; E X P E C T _ E Q ( 4 2 , f o o . G e t V a l u e ( ) ) ; Unfortunately, it doesn't work here. The above code will fail with error: V a l u e o f : f o o . G e t V a l u e ( ) A c t u a l : 0 E x p e c t e d : 4 2 The reason is that R e t u r n ( v a l u e )converts v a l u eto the actual return type of the mock function at the time when the action is created, not when it is executed. (This behavior was chosen for the action to be safe when v a l u eis a proxy object that references some temporary objects.) As a result, B y R e f ( x )is converted to an i n tvalue (instead of a c o n s t i n t & ) when the expectation is set, and R e t u r n ( B y R e f ( x ) )will always return 0. R e t u r n P o i n t e e ( p o i n t e r )was provided to solve this problem specifically. It returns the value pointed to by p o i n t e rat the time the action is executed: u s i n g t e s t i n g : : R e t u r n P o i n t e e ; . . . i n t x = 0 ; M o c k F o o f o o ; E X P E C T _ C A L L ( f o o , G e t V a l u e ( ) ) . W i l l R e p e a t e d l y ( R e t u r n P o i n t e e ( & x ) ) ; / / N o t e t h e & h e r e . x = 4 2 ; E X P E C T _ E Q ( 4 2 , f o o . G e t V a l u e ( ) ) ; / / T h i s w i l l s u c c e e d n o w . Combining Actions Want to do more than one thing when a function is called? That's fine. D o A l l ( )allow you to do sequence of actions every time. Only the return value of the last action in the sequence will be used. u s i n g : : t e s t i n g : : D o A l l ; c l a s s M o c k F o o : p u b l i c F o o { p u b l i c : M O C K _ M E T H O D 1 ( B a r , b o o l ( i n t n ) ) ; } ; . . .
  • 20. 8/23/13 CookBook - googlemock - Google C++ Mocking Framework Cookbook - Google C++ Mocking Framework - Google Project Hosting https://code.google.com/p/googlemock/wiki/CookBook 20/41 . . . E X P E C T _ C A L L ( f o o , B a r ( _ ) ) . W i l l O n c e ( D o A l l ( a c t i o n _ 1 , a c t i o n _ 2 , . . . a c t i o n _ n ) ) ; Mocking Side Effects Sometimes a method exhibits its effect not via returning a value but via side effects. For example, it may change some global state or modify an output argument. To mock side effects, in general you can define your own action by implementing : : t e s t i n g : : A c t i o n I n t e r f a c e . If all you need to do is to change an output argument, the built­in S e t A r g P o i n t e e ( )action is convenient: u s i n g : : t e s t i n g : : S e t A r g P o i n t e e ; c l a s s M o c k M u t a t o r : p u b l i c M u t a t o r { p u b l i c : M O C K _ M E T H O D 2 ( M u t a t e , v o i d ( b o o l m u t a t e , i n t * v a l u e ) ) ; . . . } ; . . . M o c k M u t a t o r m u t a t o r ; E X P E C T _ C A L L ( m u t a t o r , M u t a t e ( t r u e , _ ) ) . W i l l O n c e ( S e t A r g P o i n t e e < 1 > ( 5 ) ) ; In this example, when m u t a t o r . M u t a t e ( )is called, we will assign 5 to the i n tvariable pointed to by argument #1 (0­based). S e t A r g P o i n t e e ( )conveniently makes an internal copy of the value you pass to it, removing the need to keep the value in scope and alive. The implication however is that the value must have a copy constructor and assignment operator. If the mock method also needs to return a value as well, you can chain S e t A r g P o i n t e e ( )with R e t u r n ( )using D o A l l ( ) : u s i n g : : t e s t i n g : : _ ; u s i n g : : t e s t i n g : : R e t u r n ; u s i n g : : t e s t i n g : : S e t A r g P o i n t e e ; c l a s s M o c k M u t a t o r : p u b l i c M u t a t o r { p u b l i c : . . . M O C K _ M E T H O D 1 ( M u t a t e I n t , b o o l ( i n t * v a l u e ) ) ; } ; . . . M o c k M u t a t o r m u t a t o r ; E X P E C T _ C A L L ( m u t a t o r , M u t a t e I n t ( _ ) ) . W i l l O n c e ( D o A l l ( S e t A r g P o i n t e e < 0 > ( 5 ) , R e t u r n ( t r u e ) ) ) ; If the output argument is an array, use the S e t A r r a y A r g u m e n t < N > ( f i r s t , l a s t )action instead. It copies the elements in source range [ f i r s t , l a s t )to the array pointed to by the N ­th (0­based) argument: u s i n g : : t e s t i n g : : N o t N u l l ; u s i n g : : t e s t i n g : : S e t A r r a y A r g u m e n t ; c l a s s M o c k A r r a y M u t a t o r : p u b l i c A r r a y M u t a t o r { p u b l i c : M O C K _ M E T H O D 2 ( M u t a t e , v o i d ( i n t * v a l u e s , i n t n u m _ v a l u e s ) ) ; . . . } ; . . . M o c k A r r a y M u t a t o r m u t a t o r ; i n t v a l u e s [ 5 ] = { 1 , 2 , 3 , 4 , 5 } ; E X P E C T _ C A L L ( m u t a t o r , M u t a t e ( N o t N u l l ( ) , 5 ) ) . W i l l O n c e ( S e t A r r a y A r g u m e n t < 0 > ( v a l u e s , v a l u e s + 5 ) ) ; This also works when the argument is an output iterator: u s i n g : : t e s t i n g : : _ ;
  • 21. 8/23/13 CookBook - googlemock - Google C++ Mocking Framework Cookbook - Google C++ Mocking Framework - Google Project Hosting https://code.google.com/p/googlemock/wiki/CookBook 21/41 u s i n g : : t e s t i n g : : _ ; u s i n g : : t e s t i n g : : S e A r r a y A r g u m e n t ; c l a s s M o c k R o l o d e x : p u b l i c R o l o d e x { p u b l i c : M O C K _ M E T H O D 1 ( G e t N a m e s , v o i d ( s t d : : b a c k _ i n s e r t _ i t e r a t o r < v e c t o r < s t r i n g > > ) ) ; . . . } ; . . . M o c k R o l o d e x r o l o d e x ; v e c t o r < s t r i n g > n a m e s ; n a m e s . p u s h _ b a c k ( " G e o r g e " ) ; n a m e s . p u s h _ b a c k ( " J o h n " ) ; n a m e s . p u s h _ b a c k ( " T h o m a s " ) ; E X P E C T _ C A L L ( r o l o d e x , G e t N a m e s ( _ ) ) . W i l l O n c e ( S e t A r r a y A r g u m e n t < 0 > ( n a m e s . b e g i n ( ) , n a m e s . e n d ( ) ) ) ; Changing a Mock Object's Behavior Based on the State If you expect a call to change the behavior of a mock object, you can use : : t e s t i n g : : I n S e q u e n c eto specify different behaviors before and after the call: u s i n g : : t e s t i n g : : I n S e q u e n c e ; u s i n g : : t e s t i n g : : R e t u r n ; . . . { I n S e q u e n c e s e q ; E X P E C T _ C A L L ( m y _ m o c k , I s D i r t y ( ) ) . W i l l R e p e a t e d l y ( R e t u r n ( t r u e ) ) ; E X P E C T _ C A L L ( m y _ m o c k , F l u s h ( ) ) ; E X P E C T _ C A L L ( m y _ m o c k , I s D i r t y ( ) ) . W i l l R e p e a t e d l y ( R e t u r n ( f a l s e ) ) ; } m y _ m o c k . F l u s h I f D i r t y ( ) ; This makes m y _ m o c k . I s D i r t y ( )return t r u ebefore m y _ m o c k . F l u s h ( )is called and return f a l s eafterwards. If the behavior change is more complex, you can store the effects in a variable and make a mock method get its return value from that variable: u s i n g : : t e s t i n g : : _ ; u s i n g : : t e s t i n g : : S a v e A r g ; u s i n g : : t e s t i n g : : R e t u r n ; A C T I O N _ P ( R e t u r n P o i n t e e , p ) { r e t u r n * p ; } . . . i n t p r e v i o u s _ v a l u e = 0 ; E X P E C T _ C A L L ( m y _ m o c k , G e t P r e v V a l u e ( ) ) . W i l l R e p e a t e d l y ( R e t u r n P o i n t e e ( & p r e v i o u s _ v a l u e ) ) ; E X P E C T _ C A L L ( m y _ m o c k , U p d a t e V a l u e ( _ ) ) . W i l l R e p e a t e d l y ( S a v e A r g < 0 > ( & p r e v i o u s _ v a l u e ) ) ; m y _ m o c k . D o S o m e t h i n g T o U p d a t e V a l u e ( ) ; Here m y _ m o c k . G e t P r e v V a l u e ( )will always return the argument of the last U p d a t e V a l u e ( )call. Setting the Default Value for a Return Type If a mock method's return type is a built­in C++ type or pointer, by default it will return 0 when invoked. You only need to specify an action if this default value doesn't work for you. Sometimes, you may want to change this default value, or you may want to specify a default value for types Google Mock doesn't know about. You can do this using the : : t e s t i n g : : D e f a u l t V a l u eclass template: c l a s s M o c k F o o : p u b l i c F o o { p u b l i c : M O C K _ M E T H O D 0 ( C a l c u l a t e B a r , B a r ( ) ) ; } ; . . . B a r d e f a u l t _ b a r ; / / S e t s t h e d e f a u l t r e t u r n v a l u e f o r t y p e B a r .
  • 22. 8/23/13 CookBook - googlemock - Google C++ Mocking Framework Cookbook - Google C++ Mocking Framework - Google Project Hosting https://code.google.com/p/googlemock/wiki/CookBook 22/41 / / S e t s t h e d e f a u l t r e t u r n v a l u e f o r t y p e B a r . D e f a u l t V a l u e < B a r > : : S e t ( d e f a u l t _ b a r ) ; M o c k F o o f o o ; / / W e d o n ' t n e e d t o s p e c i f y a n a c t i o n h e r e , a s t h e d e f a u l t / / r e t u r n v a l u e w o r k s f o r u s . E X P E C T _ C A L L ( f o o , C a l c u l a t e B a r ( ) ) ; f o o . C a l c u l a t e B a r ( ) ; / / T h i s s h o u l d r e t u r n d e f a u l t _ b a r . / / U n s e t s t h e d e f a u l t r e t u r n v a l u e . D e f a u l t V a l u e < B a r > : : C l e a r ( ) ; Please note that changing the default value for a type can make you tests hard to understand. We recommend you to use this feature judiciously. For example, you may want to make sure the S e t ( )and C l e a r ( )calls are right next to the code that uses your mock. Setting the Default Actions for a Mock Method You've learned how to change the default value of a given type. However, this may be too coarse for your purpose: perhaps you have two mock methods with the same return type and you want them to have different behaviors. The O N _ C A L L ( )macro allows you to customize your mock's behavior at the method level: u s i n g : : t e s t i n g : : _ ; u s i n g : : t e s t i n g : : A n y N u m b e r ; u s i n g : : t e s t i n g : : G t ; u s i n g : : t e s t i n g : : R e t u r n ; . . . O N _ C A L L ( f o o , S i g n ( _ ) ) . W i l l B y D e f a u l t ( R e t u r n ( - 1 ) ) ; O N _ C A L L ( f o o , S i g n ( 0 ) ) . W i l l B y D e f a u l t ( R e t u r n ( 0 ) ) ; O N _ C A L L ( f o o , S i g n ( G t ( 0 ) ) ) . W i l l B y D e f a u l t ( R e t u r n ( 1 ) ) ; E X P E C T _ C A L L ( f o o , S i g n ( _ ) ) . T i m e s ( A n y N u m b e r ( ) ) ; f o o . S i g n ( 5 ) ; / / T h i s s h o u l d r e t u r n 1 . f o o . S i g n ( - 9 ) ; / / T h i s s h o u l d r e t u r n - 1 . f o o . S i g n ( 0 ) ; / / T h i s s h o u l d r e t u r n 0 . As you may have guessed, when there are more than one O N _ C A L L ( )statements, the news order take precedence over the older ones. In other words, the last one that matches the function arguments will be used. This matching order allows you to set up the common behavior in a mock object's constructor or the test fixture's set­up phase and specialize the mock's behavior later. Using Functions/Methods/Functors as Actions If the built­in actions don't suit you, you can easily use an existing function, method, or functor as an action: u s i n g : : t e s t i n g : : _ ; u s i n g : : t e s t i n g : : I n v o k e ; c l a s s M o c k F o o : p u b l i c F o o { p u b l i c : M O C K _ M E T H O D 2 ( S u m , i n t ( i n t x , i n t y ) ) ; M O C K _ M E T H O D 1 ( C o m p l e x J o b , b o o l ( i n t x ) ) ; } ; i n t C a l c u l a t e S u m ( i n t x , i n t y ) { r e t u r n x + y ; } c l a s s H e l p e r { p u b l i c : b o o l C o m p l e x J o b ( i n t x ) ; } ; . . . M o c k F o o f o o ; H e l p e r h e l p e r ; E X P E C T _ C A L L ( f o o , S u m ( _ , _ ) ) . W i l l O n c e ( I n v o k e ( C a l c u l a t e S u m ) ) ; E X P E C T _ C A L L ( f o o , C o m p l e x J o b ( _ ) )
  • 23. 8/23/13 CookBook - googlemock - Google C++ Mocking Framework Cookbook - Google C++ Mocking Framework - Google Project Hosting https://code.google.com/p/googlemock/wiki/CookBook 23/41 E X P E C T _ C A L L ( f o o , C o m p l e x J o b ( _ ) ) . W i l l O n c e ( I n v o k e ( & h e l p e r , & H e l p e r : : C o m p l e x J o b ) ) ; f o o . S u m ( 5 , 6 ) ; / / I n v o k e s C a l c u l a t e S u m ( 5 , 6 ) . f o o . C o m p l e x J o b ( 1 0 ) ; / / I n v o k e s h e l p e r . C o m p l e x J o b ( 1 0 ) ; The only requirement is that the type of the function, etc must be compatible with the signature of the mock function, meaning that the latter's arguments can be implicitly converted to the corresponding arguments of the former, and the former's return type can be implicitly converted to that of the latter. So, you can invoke something whose type is not exactly the same as the mock function, as long as it's safe to do so ­ nice, huh? Invoking a Function/Method/Functor Without Arguments I n v o k e ( )is very useful for doing actions that are more complex. It passes the mock function's arguments to the function or functor being invoked such that the callee has the full context of the call to work with. If the invoked function is not interested in some or all of the arguments, it can simply ignore them. Yet, a common pattern is that a test author wants to invoke a function without the arguments of the mock function. I n v o k e ( )allows her to do that using a wrapper function that throws away the arguments before invoking an underlining nullary function. Needless to say, this can be tedious and obscures the intent of the test. I n v o k e W i t h o u t A r g s ( )solves this problem. It's like I n v o k e ( )except that it doesn't pass the mock function's arguments to the callee. Here's an example: u s i n g : : t e s t i n g : : _ ; u s i n g : : t e s t i n g : : I n v o k e W i t h o u t A r g s ; c l a s s M o c k F o o : p u b l i c F o o { p u b l i c : M O C K _ M E T H O D 1 ( C o m p l e x J o b , b o o l ( i n t n ) ) ; } ; b o o l J o b 1 ( ) { . . . } . . . M o c k F o o f o o ; E X P E C T _ C A L L ( f o o , C o m p l e x J o b ( _ ) ) . W i l l O n c e ( I n v o k e W i t h o u t A r g s ( J o b 1 ) ) ; f o o . C o m p l e x J o b ( 1 0 ) ; / / I n v o k e s J o b 1 ( ) . Invoking an Argument of the Mock Function Sometimes a mock function will receive a function pointer or a functor (in other words, a "callable") as an argument, e.g. c l a s s M o c k F o o : p u b l i c F o o { p u b l i c : M O C K _ M E T H O D 2 ( D o T h i s , b o o l ( i n t n , b o o l ( * f p ) ( i n t ) ) ) ; } ; and you may want to invoke this callable argument: u s i n g : : t e s t i n g : : _ ; . . . M o c k F o o f o o ; E X P E C T _ C A L L ( f o o , D o T h i s ( _ , _ ) ) . W i l l O n c e ( . . . ) ; / / W i l l e x e c u t e ( * f p ) ( 5 ) , w h e r e f p i s t h e / / s e c o n d a r g u m e n t D o T h i s ( ) r e c e i v e s . Arghh, you need to refer to a mock function argument but C++ has no lambda (yet), so you have to define your own action. :­( Or do you really? Well, Google Mock has an action to solve exactly this problem: I n v o k e A r g u m e n t < N > ( a r g _ 1 , a r g _ 2 , . . . , a r g _ m ) will invoke the N ­th (0­based) argument the mock function receives, with a r g _ 1 , a r g _ 2 , ..., and a r g _ m . No matter if the argument is a function pointer or a functor, Google Mock handles them both. With that, you could write: u s i n g : : t e s t i n g : : _ ; u s i n g : : t e s t i n g : : I n v o k e A r g u m e n t ;
  • 24. 8/23/13 CookBook - googlemock - Google C++ Mocking Framework Cookbook - Google C++ Mocking Framework - Google Project Hosting https://code.google.com/p/googlemock/wiki/CookBook 24/41 u s i n g : : t e s t i n g : : I n v o k e A r g u m e n t ; . . . E X P E C T _ C A L L ( f o o , D o T h i s ( _ , _ ) ) . W i l l O n c e ( I n v o k e A r g u m e n t < 1 > ( 5 ) ) ; / / W i l l e x e c u t e ( * f p ) ( 5 ) , w h e r e f p i s t h e / / s e c o n d a r g u m e n t D o T h i s ( ) r e c e i v e s . What if the callable takes an argument by reference? No problem ­ just wrap it inside B y R e f ( ) : . . . M O C K _ M E T H O D 1 ( B a r , b o o l ( b o o l ( * f p ) ( i n t , c o n s t H e l p e r & ) ) ) ; . . . u s i n g : : t e s t i n g : : _ ; u s i n g : : t e s t i n g : : B y R e f ; u s i n g : : t e s t i n g : : I n v o k e A r g u m e n t ; . . . M o c k F o o f o o ; H e l p e r h e l p e r ; . . . E X P E C T _ C A L L ( f o o , B a r ( _ ) ) . W i l l O n c e ( I n v o k e A r g u m e n t < 0 > ( 5 , B y R e f ( h e l p e r ) ) ) ; / / B y R e f ( h e l p e r ) g u a r a n t e e s t h a t a r e f e r e n c e t o h e l p e r , n o t a c o p y o f i t , / / w i l l b e p a s s e d t o t h e c a l l a b l e . What if the callable takes an argument by reference and we do not wrap the argument in B y R e f ( ) ? Then I n v o k e A r g u m e n t ( )will make a copy of the argument, and pass a reference to the copy, instead of a reference to the original value, to the callable. This is especially handy when the argument is a temporary value: . . . M O C K _ M E T H O D 1 ( D o T h a t , b o o l ( b o o l ( * f ) ( c o n s t d o u b l e & x , c o n s t s t r i n g & s ) ) ) ; . . . u s i n g : : t e s t i n g : : _ ; u s i n g : : t e s t i n g : : I n v o k e A r g u m e n t ; . . . M o c k F o o f o o ; . . . E X P E C T _ C A L L ( f o o , D o T h a t ( _ ) ) . W i l l O n c e ( I n v o k e A r g u m e n t < 0 > ( 5 . 0 , s t r i n g ( " H i " ) ) ) ; / / W i l l e x e c u t e ( * f ) ( 5 . 0 , s t r i n g ( " H i " ) ) , w h e r e f i s t h e f u n c t i o n p o i n t e r / / D o T h a t ( ) r e c e i v e s . N o t e t h a t t h e v a l u e s 5 . 0 a n d s t r i n g ( " H i " ) a r e / / t e m p o r a r y a n d d e a d o n c e t h e E X P E C T _ C A L L ( ) s t a t e m e n t f i n i s h e s . Y e t / / i t ' s f i n e t o p e r f o r m t h i s a c t i o n l a t e r , s i n c e a c o p y o f t h e v a l u e s / / a r e k e p t i n s i d e t h e I n v o k e A r g u m e n t a c t i o n . Ignoring an Action's Result Sometimes you have an action that returns something, but you need an action that returns v o i d(perhaps you want to use it in a mock function that returns v o i d , or perhaps it needs to be used in D o A l l ( )and it's not the last in the list). I g n o r e R e s u l t ( )lets you do that. For example: u s i n g : : t e s t i n g : : _ ; u s i n g : : t e s t i n g : : I n v o k e ; u s i n g : : t e s t i n g : : R e t u r n ; i n t P r o c e s s ( c o n s t M y D a t a & d a t a ) ; s t r i n g D o S o m e t h i n g ( ) ; c l a s s M o c k F o o : p u b l i c F o o { p u b l i c : M O C K _ M E T H O D 1 ( A b c , v o i d ( c o n s t M y D a t a & d a t a ) ) ; M O C K _ M E T H O D 0 ( X y z , b o o l ( ) ) ; } ; . . . M o c k F o o f o o ; E X P E C T _ C A L L ( f o o , A b c ( _ ) ) / / . W i l l O n c e ( I n v o k e ( P r o c e s s ) ) ; / / T h e a b o v e l i n e w o n ' t c o m p i l e a s P r o c e s s ( ) r e t u r n s i n t b u t A b c ( ) n e e d s / / t o r e t u r n v o i d . . W i l l O n c e ( I g n o r e R e s u l t ( I n v o k e ( P r o c e s s ) ) ) ;
  • 25. 8/23/13 CookBook - googlemock - Google C++ Mocking Framework Cookbook - Google C++ Mocking Framework - Google Project Hosting https://code.google.com/p/googlemock/wiki/CookBook 25/41 . W i l l O n c e ( I g n o r e R e s u l t ( I n v o k e ( P r o c e s s ) ) ) ; E X P E C T _ C A L L ( f o o , X y z ( ) ) . W i l l O n c e ( D o A l l ( I g n o r e R e s u l t ( I n v o k e ( D o S o m e t h i n g ) ) , / / I g n o r e s t h e s t r i n g D o S o m e t h i n g ( ) r e t u r n s . R e t u r n ( t r u e ) ) ) ; Note that you cannot use I g n o r e R e s u l t ( )on an action that already returns v o i d . Doing so will lead to ugly compiler errors. Selecting an Action's Arguments Say you have a mock function F o o ( )that takes seven arguments, and you have a custom action that you want to invoke when F o o ( )is called. Trouble is, the custom action only wants three arguments: u s i n g : : t e s t i n g : : _ ; u s i n g : : t e s t i n g : : I n v o k e ; . . . M O C K _ M E T H O D 7 ( F o o , b o o l ( b o o l v i s i b l e , c o n s t s t r i n g & n a m e , i n t x , i n t y , c o n s t m a p < p a i r < i n t , i n t > , d o u b l e > & w e i g h t , d o u b l e m i n _ w e i g h t , d o u b l e m a x _ w i g h t ) ) ; . . . b o o l I s V i s i b l e I n Q u a d r a n t 1 ( b o o l v i s i b l e , i n t x , i n t y ) { r e t u r n v i s i b l e & & x > = 0 & & y > = 0 ; } . . . E X P E C T _ C A L L ( m o c k , F o o ( _ , _ , _ , _ , _ , _ , _ ) ) . W i l l O n c e ( I n v o k e ( I s V i s i b l e I n Q u a d r a n t 1 ) ) ; / / U h , w o n ' t c o m p i l e . : - ( To please the compiler God, you can to define an "adaptor" that has the same signature as F o o ( )and calls the custom action with the right arguments: u s i n g : : t e s t i n g : : _ ; u s i n g : : t e s t i n g : : I n v o k e ; b o o l M y I s V i s i b l e I n Q u a d r a n t 1 ( b o o l v i s i b l e , c o n s t s t r i n g & n a m e , i n t x , i n t y , c o n s t m a p < p a i r < i n t , i n t > , d o u b l e > & w e i g h t , d o u b l e m i n _ w e i g h t , d o u b l e m a x _ w i g h t ) { r e t u r n I s V i s i b l e I n Q u a d r a n t 1 ( v i s i b l e , x , y ) ; } . . . E X P E C T _ C A L L ( m o c k , F o o ( _ , _ , _ , _ , _ , _ , _ ) ) . W i l l O n c e ( I n v o k e ( M y I s V i s i b l e I n Q u a d r a n t 1 ) ) ; / / N o w i t w o r k s . But isn't this awkward? Google Mock provides a generic action adaptor, so you can spend your time minding more important business than writing your own adaptors. Here's the syntax: W i t h A r g s < N 1 , N 2 , . . . , N k > ( a c t i o n ) creates an action that passes the arguments of the mock function at the given indices (0­based) to the inner a c t i o nand performs it. Using W i t h A r g s , our original example can be written as: u s i n g : : t e s t i n g : : _ ; u s i n g : : t e s t i n g : : I n v o k e ; u s i n g : : t e s t i n g : : W i t h A r g s ; . . . E X P E C T _ C A L L ( m o c k , F o o ( _ , _ , _ , _ , _ , _ , _ ) ) . W i l l O n c e ( W i t h A r g s < 0 , 2 , 3 > ( I n v o k e ( I s V i s i b l e I n Q u a d r a n t 1 ) ) ) ; / / N o n e e d t o d e f i n e y o u r o w n a d a p t o r . For better readability, Google Mock also gives you: W i t h o u t A r g s ( a c t i o n )when the inner a c t i o ntakes no argument, and W i t h A r g < N > ( a c t i o n )(no safter A r g ) when the inner a c t i o ntakes one argument. As you may have realized, I n v o k e W i t h o u t A r g s ( . . . )is just syntactic sugar for W i t h o u t A r g s ( I n o v k e ( . . . ) ) . Here are more tips:
  • 26. 8/23/13 CookBook - googlemock - Google C++ Mocking Framework Cookbook - Google C++ Mocking Framework - Google Project Hosting https://code.google.com/p/googlemock/wiki/CookBook 26/41 Here are more tips: The inner action used in W i t h A r g sand friends does not have to be I n v o k e ( )­­ it can be anything. You can repeat an argument in the argument list if necessary, e.g. W i t h A r g s < 2 , 3 , 3 , 5 > ( . . . ) . You can change the order of the arguments, e.g. W i t h A r g s < 3 , 2 , 1 > ( . . . ) . The types of the selected arguments do not have to match the signature of the inner action exactly. It works as long as they can be implicitly converted to the corresponding arguments of the inner action. For example, if the 4­th argument of the mock function is an i n tand m y _ a c t i o ntakes a d o u b l e , W i t h A r g < 4 > ( m y _ a c t i o n )will work. Ignoring Arguments in Action Functions The selecting­an­action's­arguments recipe showed us one way to make a mock function and an action with incompatible argument lists fit together. The downside is that wrapping the action in W i t h A r g s < . . . > ( )can get tedious for people writing the tests. If you are defining a function, method, or functor to be used with I n v o k e * ( ) , and you are not interested in some of its arguments, an alternative to W i t h A r g sis to declare the uninteresting arguments as U n u s e d . This makes the definition less cluttered and less fragile in case the types of the uninteresting arguments change. It could also increase the chance the action function can be reused. For example, given M O C K _ M E T H O D 3 ( F o o , d o u b l e ( c o n s t s t r i n g & l a b e l , d o u b l e x , d o u b l e y ) ) ; M O C K _ M E T H O D 3 ( B a r , d o u b l e ( i n t i n d e x , d o u b l e x , d o u b l e y ) ) ; instead of u s i n g : : t e s t i n g : : _ ; u s i n g : : t e s t i n g : : I n v o k e ; d o u b l e D i s t a n c e T o O r i g i n W i t h L a b e l ( c o n s t s t r i n g & l a b e l , d o u b l e x , d o u b l e y ) { r e t u r n s q r t ( x * x + y * y ) ; } d o u b l e D i s t a n c e T o O r i g i n W i t h I n d e x ( i n t i n d e x , d o u b l e x , d o u b l e y ) { r e t u r n s q r t ( x * x + y * y ) ; } . . . E X E P C T _ C A L L ( m o c k , F o o ( " a b c " , _ , _ ) ) . W i l l O n c e ( I n v o k e ( D i s t a n c e T o O r i g i n W i t h L a b e l ) ) ; E X E P C T _ C A L L ( m o c k , B a r ( 5 , _ , _ ) ) . W i l l O n c e ( I n v o k e ( D i s t a n c e T o O r i g i n W i t h I n d e x ) ) ; you could write u s i n g : : t e s t i n g : : _ ; u s i n g : : t e s t i n g : : I n v o k e ; u s i n g : : t e s t i n g : : U n u s e d ; d o u b l e D i s t a n c e T o O r i g i n ( U n u s e d , d o u b l e x , d o u b l e y ) { r e t u r n s q r t ( x * x + y * y ) ; } . . . E X E P C T _ C A L L ( m o c k , F o o ( " a b c " , _ , _ ) ) . W i l l O n c e ( I n v o k e ( D i s t a n c e T o O r i g i n ) ) ; E X E P C T _ C A L L ( m o c k , B a r ( 5 , _ , _ ) ) . W i l l O n c e ( I n v o k e ( D i s t a n c e T o O r i g i n ) ) ; Sharing Actions Just like matchers, a Google Mock action object consists of a pointer to a ref­counted implementation object. Therefore copying actions is also allowed and very efficient. When the last action that references the implementation object dies, the implementation object will be deleted. If you have some complex action that you want to use again and again, you may not have to build it from scratch everytime. If the action doesn't have an internal state (i.e. if it always does the same thing no matter how many times it has been called), you can assign it to an action variable and use that variable repeatedly. For example: A c t i o n < b o o l ( i n t * ) > s e t _ f l a g = D o A l l ( S e t A r g P o i n t e e < 0 > ( 5 ) , R e t u r n ( t r u e ) ) ; . . . u s e s e t _ f l a g i n . W i l l O n c e ( ) a n d . W i l l R e p e a t e d l y ( ) . . . However, if the action has its own state, you may be surprised if you share the action object. Suppose you have an action factory I n c r e m e n t C o u n t e r ( i n i t )which creates an action that increments and returns a counter whose initial value is i n i t , using two actions created
  • 27. 8/23/13 CookBook - googlemock - Google C++ Mocking Framework Cookbook - Google C++ Mocking Framework - Google Project Hosting https://code.google.com/p/googlemock/wiki/CookBook 27/41 from the same expression and using a shared action will exihibit different behaviors. Example: E X P E C T _ C A L L ( f o o , D o T h i s ( ) ) . W i l l R e p e a t e d l y ( I n c r e m e n t C o u n t e r ( 0 ) ) ; E X P E C T _ C A L L ( f o o , D o T h a t ( ) ) . W i l l R e p e a t e d l y ( I n c r e m e n t C o u n t e r ( 0 ) ) ; f o o . D o T h i s ( ) ; / / R e t u r n s 1 . f o o . D o T h i s ( ) ; / / R e t u r n s 2 . f o o . D o T h a t ( ) ; / / R e t u r n s 1 - B l a h ( ) u s e s a d i f f e r e n t / / c o u n t e r t h a n B a r ( ) ' s . versus A c t i o n < i n t ( ) > i n c r e m e n t = I n c r e m e n t C o u n t e r ( 0 ) ; E X P E C T _ C A L L ( f o o , D o T h i s ( ) ) . W i l l R e p e a t e d l y ( i n c r e m e n t ) ; E X P E C T _ C A L L ( f o o , D o T h a t ( ) ) . W i l l R e p e a t e d l y ( i n c r e m e n t ) ; f o o . D o T h i s ( ) ; / / R e t u r n s 1 . f o o . D o T h i s ( ) ; / / R e t u r n s 2 . f o o . D o T h a t ( ) ; / / R e t u r n s 3 - t h e c o u n t e r i s s h a r e d . Misc Recipes on Using Google Mock Making the Compilation Faster Believe it or not, the vast majority of the time spent on compiling a mock class is in generating its constructor and destructor, as they perform non­trivial tasks (e.g. verification of the expectations). What's more, mock methods with different signatures have different types and thus their constructors/destructors need to be generated by the compiler separately. As a result, if you mock many different types of methods, compiling your mock class can get really slow. If you are experiencing slow compilation, you can move the definition of your mock class' constructor and destructor out of the class body and into a . c p pfile. This way, even if you # i n c l u d eyour mock class in N files, the compiler only needs to generate its constructor and destructor once, resulting in a much faster compilation. Let's illustrate the idea using an example. Here's the definition of a mock class before applying this recipe: / / F i l e m o c k _ f o o . h . . . . c l a s s M o c k F o o : p u b l i c F o o { p u b l i c : / / S i n c e w e d o n ' t d e c l a r e t h e c o n s t r u c t o r o r t h e d e s t r u c t o r , / / t h e c o m p i l e r w i l l g e n e r a t e t h e m i n e v e r y t r a n s l a t i o n u n i t / / w h e r e t h i s m o c k c l a s s i s u s e d . M O C K _ M E T H O D 0 ( D o T h i s , i n t ( ) ) ; M O C K _ M E T H O D 1 ( D o T h a t , b o o l ( c o n s t c h a r * s t r ) ) ; . . . m o r e m o c k m e t h o d s . . . } ; After the change, it would look like: / / F i l e m o c k _ f o o . h . . . . c l a s s M o c k F o o : p u b l i c F o o { p u b l i c : / / T h e c o n s t r u c t o r a n d d e s t r u c t o r a r e d e c l a r e d , b u t n o t d e f i n e d , h e r e . M o c k F o o ( ) ; v i r t u a l ~ M o c k F o o ( ) ; M O C K _ M E T H O D 0 ( D o T h i s , i n t ( ) ) ; M O C K _ M E T H O D 1 ( D o T h a t , b o o l ( c o n s t c h a r * s t r ) ) ; . . . m o r e m o c k m e t h o d s . . . } ; and / / F i l e m o c k _ f o o . c p p . # i n c l u d e " p a t h / t o / m o c k _ f o o . h "