Googletest export

Docs: Clarify that expectations must be set before mocks are exercised
PiperOrigin-RevId: 373644072
This commit is contained in:
Abseil Team 2021-05-13 13:15:34 -07:00 committed by Dino Radaković
parent 662fe38e44
commit eb6e9273dc
3 changed files with 25 additions and 29 deletions

View File

@ -344,6 +344,11 @@ Mock::VerifyAndClearExpectations(&mock_obj);
Mock::VerifyAndClear(&mock_obj); Mock::VerifyAndClear(&mock_obj);
``` ```
Do not set new expectations after verifying and clearing a mock after its use.
Setting expectations after code that exercises the mock has undefined behavior.
See [Using Mocks in Tests](gmock_for_dummies.md#using-mocks-in-tests) for more
information.
You can also tell gMock that a mock object can be leaked and doesn't need to be You can also tell gMock that a mock object can be leaked and doesn't need to be
verified: verified:

View File

@ -3017,31 +3017,21 @@ indicate whether the verification was successful (`true` for yes), so you can
wrap that function call inside a `ASSERT_TRUE()` if there is no point going wrap that function call inside a `ASSERT_TRUE()` if there is no point going
further when the verification has failed. further when the verification has failed.
### Using Check Points {#UsingCheckPoints} Do not set new expectations after verifying and clearing a mock after its use.
Setting expectations after code that exercises the mock has undefined behavior.
See [Using Mocks in Tests](gmock_for_dummies.md#using-mocks-in-tests) for more
information.
Sometimes you may want to "reset" a mock object at various check points in your ### Using Checkpoints {#UsingCheckPoints}
test: at each check point, you verify that all existing expectations on the mock
object have been satisfied, and then you set some new expectations on it as if
it's newly created. This allows you to work with a mock object in "phases" whose
sizes are each manageable.
One such scenario is that in your test's `SetUp()` function, you may want to put Sometimes you might want to test a mock object's behavior in phases whose sizes
the object you are testing into a certain state, with the help from a mock are each manageable, or you might want to set more detailed expectations about
object. Once in the desired state, you want to clear all expectations on the which API calls invoke which mock functions.
mock, such that in the `TEST_F` body you can set fresh expectations on it.
As you may have figured out, the `Mock::VerifyAndClearExpectations()` function A technique you can use is to put the expectations in a sequence and insert
we saw in the previous recipe can help you here. Or, if you are using calls to a dummy "checkpoint" function at specific places. Then you can verify
`ON_CALL()` to set default actions on the mock object and want to clear the that the mock function calls do happen at the right time. For example, if you
default actions as well, use `Mock::VerifyAndClear(&mock_object)` instead. This are exercising the code:
function does what `Mock::VerifyAndClearExpectations(&mock_object)` does and
returns the same `bool`, **plus** it clears the `ON_CALL()` statements on
`mock_object` too.
Another trick you can use to achieve the same effect is to put the expectations
in sequences and insert calls to a dummy "check-point" function at specific
places. Then you can verify that the mock function calls do happen at the right
time. For example, if you are exercising code:
```cpp ```cpp
Foo(1); Foo(1);
@ -3050,7 +3040,7 @@ time. For example, if you are exercising code:
``` ```
and want to verify that `Foo(1)` and `Foo(3)` both invoke `mock.Bar("a")`, but and want to verify that `Foo(1)` and `Foo(3)` both invoke `mock.Bar("a")`, but
`Foo(2)` doesn't invoke anything. You can write: `Foo(2)` doesn't invoke anything, you can write:
```cpp ```cpp
using ::testing::MockFunction; using ::testing::MockFunction;
@ -3076,10 +3066,10 @@ TEST(FooTest, InvokesBarCorrectly) {
} }
``` ```
The expectation spec says that the first `Bar("a")` must happen before check The expectation spec says that the first `Bar("a")` call must happen before
point "1", the second `Bar("a")` must happen after check point "2", and nothing checkpoint "1", the second `Bar("a")` call must happen after checkpoint "2", and
should happen between the two check points. The explicit check points make it nothing should happen between the two checkpoints. The explicit checkpoints make
easy to tell which `Bar("a")` is called by which call to `Foo()`. it clear which `Bar("a")` is called by which call to `Foo()`.
### Mocking Destructors ### Mocking Destructors

View File

@ -262,8 +262,9 @@ when you allocate mocks on the heap. You get that automatically if you use the
`gtest_main` library already. `gtest_main` library already.
**Important note:** gMock requires expectations to be set **before** the mock **Important note:** gMock requires expectations to be set **before** the mock
functions are called, otherwise the behavior is **undefined**. In particular, functions are called, otherwise the behavior is **undefined**. Do not alternate
you mustn't interleave `EXPECT_CALL()s` and calls to the mock functions. between calls to `EXPECT_CALL()` and calls to the mock functions, and do not set
any expectations on a mock after passing the mock to an API.
This means `EXPECT_CALL()` should be read as expecting that a call will occur This means `EXPECT_CALL()` should be read as expecting that a call will occur
*in the future*, not that a call has occurred. Why does gMock work like that? *in the future*, not that a call has occurred. Why does gMock work like that?