Commit e5613588 by Abseil Team Committed by Mark Barolak

Googletest export

Normalize headers in gMock docs Increasing Header levels on two pages, to better match other pages in the same directory. PiperOrigin-RevId: 318280892
parent 05c4a036
## gMock for Dummies {#GMockForDummies} # gMock for Dummies {#GMockForDummies}
<!-- GOOGLETEST_CM0013 DO NOT DELETE --> <!-- GOOGLETEST_CM0013 DO NOT DELETE -->
### What Is gMock? ## What Is gMock?
When you write a prototype or test, often it's not feasible or wise to rely on When you write a prototype or test, often it's not feasible or wise to rely on
real objects entirely. A **mock object** implements the same interface as a real real objects entirely. A **mock object** implements the same interface as a real
...@@ -39,7 +39,7 @@ When using gMock, ...@@ -39,7 +39,7 @@ When using gMock,
3. then you exercise code that uses the mock objects. gMock will catch any 3. then you exercise code that uses the mock objects. gMock will catch any
violation to the expectations as soon as it arises. violation to the expectations as soon as it arises.
### Why gMock? ## Why gMock?
While mock objects help you remove unnecessary dependencies in tests and make While mock objects help you remove unnecessary dependencies in tests and make
them fast and reliable, using mocks manually in C++ is *hard*: them fast and reliable, using mocks manually in C++ is *hard*:
...@@ -85,11 +85,11 @@ We encourage you to use gMock as ...@@ -85,11 +85,11 @@ We encourage you to use gMock as
* a *testing* tool to cut your tests' outbound dependencies and probe the * a *testing* tool to cut your tests' outbound dependencies and probe the
interaction between your module and its collaborators. interaction between your module and its collaborators.
### Getting Started ## Getting Started
gMock is bundled with googletest. gMock is bundled with googletest.
### A Case for Mock Turtles ## A Case for Mock Turtles
Let's look at an example. Suppose you are developing a graphics program that Let's look at an example. Suppose you are developing a graphics program that
relies on a [LOGO](http://en.wikipedia.org/wiki/Logo_programming_language)-like relies on a [LOGO](http://en.wikipedia.org/wiki/Logo_programming_language)-like
...@@ -135,13 +135,13 @@ because your new machine does anti-aliasing differently), easier to read and ...@@ -135,13 +135,13 @@ because your new machine does anti-aliasing differently), easier to read and
maintain (the intent of a test is expressed in the code, not in some binary maintain (the intent of a test is expressed in the code, not in some binary
images), and run *much, much faster*. images), and run *much, much faster*.
### Writing the Mock Class ## Writing the Mock Class
If you are lucky, the mocks you need to use have already been implemented by If you are lucky, the mocks you need to use have already been implemented by
some nice people. If, however, you find yourself in the position to write a mock some nice people. If, however, you find yourself in the position to write a mock
class, relax - gMock turns this task into a fun game! (Well, almost.) class, relax - gMock turns this task into a fun game! (Well, almost.)
#### How to Define It ### How to Define It
Using the `Turtle` interface as example, here are the simple steps you need to Using the `Turtle` interface as example, here are the simple steps you need to
follow: follow:
...@@ -184,7 +184,7 @@ class MockTurtle : public Turtle { ...@@ -184,7 +184,7 @@ class MockTurtle : public Turtle {
You don't need to define these mock methods somewhere else - the `MOCK_METHOD` You don't need to define these mock methods somewhere else - the `MOCK_METHOD`
macro will generate the definitions for you. It's that simple! macro will generate the definitions for you. It's that simple!
#### Where to Put It ### Where to Put It
When you define a mock class, you need to decide where to put its definition. When you define a mock class, you need to decide where to put its definition.
Some people put it in a `_test.cc`. This is fine when the interface being mocked Some people put it in a `_test.cc`. This is fine when the interface being mocked
...@@ -208,7 +208,7 @@ specific domain much better than `Foo` does. ...@@ -208,7 +208,7 @@ specific domain much better than `Foo` does.
<!-- GOOGLETEST_CM0029 DO NOT DELETE --> <!-- GOOGLETEST_CM0029 DO NOT DELETE -->
### Using Mocks in Tests ## Using Mocks in Tests
Once you have a mock class, using it is easy. The typical work flow is: Once you have a mock class, using it is easy. The typical work flow is:
...@@ -279,7 +279,7 @@ Admittedly, this test is contrived and doesn't do much. You can easily achieve ...@@ -279,7 +279,7 @@ Admittedly, this test is contrived and doesn't do much. You can easily achieve
the same effect without using gMock. However, as we shall reveal soon, gMock the same effect without using gMock. However, as we shall reveal soon, gMock
allows you to do *so much more* with the mocks. allows you to do *so much more* with the mocks.
### Setting Expectations ## Setting Expectations
The key to using a mock object successfully is to set the *right expectations* The key to using a mock object successfully is to set the *right expectations*
on it. If you set the expectations too strict, your test will fail as the result on it. If you set the expectations too strict, your test will fail as the result
...@@ -288,7 +288,7 @@ to do it just right such that your test can catch exactly the kind of bugs you ...@@ -288,7 +288,7 @@ to do it just right such that your test can catch exactly the kind of bugs you
intend it to catch. gMock provides the necessary means for you to do it "just intend it to catch. gMock provides the necessary means for you to do it "just
right." right."
#### General Syntax ### General Syntax
In gMock we use the `EXPECT_CALL()` macro to set an expectation on a mock In gMock we use the `EXPECT_CALL()` macro to set an expectation on a mock
method. The general syntax is: method. The general syntax is:
...@@ -343,7 +343,7 @@ it makes expectations easily identifiable (either by `gsearch` or by a human ...@@ -343,7 +343,7 @@ it makes expectations easily identifiable (either by `gsearch` or by a human
reader), and second it allows gMock to include the source file location of a reader), and second it allows gMock to include the source file location of a
failed expectation in messages, making debugging easier. failed expectation in messages, making debugging easier.
#### Matchers: What Arguments Do We Expect? ### Matchers: What Arguments Do We Expect?
When a mock function takes arguments, we may specify what arguments we are When a mock function takes arguments, we may specify what arguments we are
expecting, for example: expecting, for example:
...@@ -399,7 +399,7 @@ to help gMock resolve which overload is expected by specifying the number of ...@@ -399,7 +399,7 @@ to help gMock resolve which overload is expected by specifying the number of
arguments and possibly also the arguments and possibly also the
[types of the arguments](cook_book.md#SelectOverload). [types of the arguments](cook_book.md#SelectOverload).
#### Cardinalities: How Many Times Will It Be Called? ### Cardinalities: How Many Times Will It Be Called?
The first clause we can specify following an `EXPECT_CALL()` is `Times()`. We The first clause we can specify following an `EXPECT_CALL()` is `Times()`. We
call its argument a **cardinality** as it tells *how many times* the call should call its argument a **cardinality** as it tells *how many times* the call should
...@@ -429,7 +429,7 @@ the cardinality for you.** The rules are easy to remember: ...@@ -429,7 +429,7 @@ the cardinality for you.** The rules are easy to remember:
**Quick quiz:** what do you think will happen if a function is expected to be **Quick quiz:** what do you think will happen if a function is expected to be
called twice but actually called four times? called twice but actually called four times?
#### Actions: What Should It Do? ### Actions: What Should It Do?
Remember that a mock object doesn't really have a working implementation? We as Remember that a mock object doesn't really have a working implementation? We as
users have to tell it what to do when a method is invoked. This is easy in users have to tell it what to do when a method is invoked. This is easy in
...@@ -522,7 +522,7 @@ will be taken afterwards. So the right answer is that `turtle.GetY()` will ...@@ -522,7 +522,7 @@ will be taken afterwards. So the right answer is that `turtle.GetY()` will
return 100 the first time, but **return 0 from the second time on**, as return 100 the first time, but **return 0 from the second time on**, as
returning 0 is the default action for `int` functions. returning 0 is the default action for `int` functions.
#### Using Multiple Expectations {#MultiExpectations} ### Using Multiple Expectations {#MultiExpectations}
So far we've only shown examples where you have a single expectation. More So far we've only shown examples where you have a single expectation. More
realistically, you'll specify expectations on multiple mock methods which may be realistically, you'll specify expectations on multiple mock methods which may be
...@@ -563,7 +563,7 @@ useful for methods that have some expectations, but for which other calls are ...@@ -563,7 +563,7 @@ useful for methods that have some expectations, but for which other calls are
ok. See ok. See
[Understanding Uninteresting vs Unexpected Calls](cook_book.md#uninteresting-vs-unexpected). [Understanding Uninteresting vs Unexpected Calls](cook_book.md#uninteresting-vs-unexpected).
#### Ordered vs Unordered Calls {#OrderedCalls} ### Ordered vs Unordered Calls {#OrderedCalls}
By default, an expectation can match a call even though an earlier expectation By default, an expectation can match a call even though an earlier expectation
hasn't been satisfied. In other words, the calls don't have to occur in the hasn't been satisfied. In other words, the calls don't have to occur in the
...@@ -600,7 +600,7 @@ order as written. If a call is made out-of-order, it will be an error. ...@@ -600,7 +600,7 @@ order as written. If a call is made out-of-order, it will be an error.
them? Can you specify an arbitrary partial order? The answer is ... yes! The them? Can you specify an arbitrary partial order? The answer is ... yes! The
details can be found [here](cook_book.md#OrderedCalls).) details can be found [here](cook_book.md#OrderedCalls).)
#### All Expectations Are Sticky (Unless Said Otherwise) {#StickyExpectations} ### All Expectations Are Sticky (Unless Said Otherwise) {#StickyExpectations}
Now let's do a quick quiz to see how well you can use this mock stuff already. Now let's do a quick quiz to see how well you can use this mock stuff already.
How would you test that the turtle is asked to go to the origin *exactly twice* How would you test that the turtle is asked to go to the origin *exactly twice*
...@@ -688,7 +688,7 @@ it's in a sequence - as soon as another expectation that comes after it in the ...@@ -688,7 +688,7 @@ it's in a sequence - as soon as another expectation that comes after it in the
sequence has been used, it automatically retires (and will never be used to sequence has been used, it automatically retires (and will never be used to
match any call). match any call).
#### Uninteresting Calls ### Uninteresting Calls
A mock object may have many methods, and not all of them are that interesting. A mock object may have many methods, and not all of them are that interesting.
For example, in some tests we may not care about how many times `GetX()` and For example, in some tests we may not care about how many times `GetX()` and
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment