diff options
author | Abseil Team <absl-team@google.com> | 2023-04-17 16:30:58 (GMT) |
---|---|---|
committer | Copybara-Service <copybara-worker@google.com> | 2023-04-17 16:31:34 (GMT) |
commit | 922e0b7d805f5668b68762552f5cca9b3225acd5 (patch) | |
tree | 526a963d4cbd2c0f0610392ec75175c0195f64b7 /docs/gmock_cook_book.md | |
parent | 12a5852e451baabc79c63a86c634912c563d57bc (diff) | |
download | googletest-922e0b7d805f5668b68762552f5cca9b3225acd5.zip googletest-922e0b7d805f5668b68762552f5cca9b3225acd5.tar.gz googletest-922e0b7d805f5668b68762552f5cca9b3225acd5.tar.bz2 |
Update gMock Cookbook to reflect deprecation of testing::ByMove
PiperOrigin-RevId: 524868227
Change-Id: I702ede27570e3d3f06d534d6ccf8b39689105d07
Diffstat (limited to 'docs/gmock_cook_book.md')
-rw-r--r-- | docs/gmock_cook_book.md | 31 |
1 files changed, 13 insertions, 18 deletions
diff --git a/docs/gmock_cook_book.md b/docs/gmock_cook_book.md index d11282f..d57be58 100644 --- a/docs/gmock_cook_book.md +++ b/docs/gmock_cook_book.md @@ -2781,26 +2781,21 @@ action: If you are not happy with the default action, you can tweak it as usual; see [Setting Default Actions](#OnCall). -If you just need to return a pre-defined move-only value, you can use the -`Return(ByMove(...))` action: +If you just need to return a move-only value, you can use it in combination with +`WillOnce`. For example: ```cpp - // When this fires, the unique_ptr<> specified by ByMove(...) will - // be returned. - EXPECT_CALL(mock_buzzer_, MakeBuzz("world")) - .WillOnce(Return(ByMove(std::make_unique<Buzz>(AccessLevel::kInternal)))); - - EXPECT_NE(nullptr, mock_buzzer_.MakeBuzz("world")); + EXPECT_CALL(mock_buzzer_, MakeBuzz("hello")) + .WillOnce(Return(std::make_unique<Buzz>(AccessLevel::kInternal))); + EXPECT_NE(nullptr, mock_buzzer_.MakeBuzz("hello")); ``` -Note that `ByMove()` is essential here - if you drop it, the code won’t compile. - -Quiz time! What do you think will happen if a `Return(ByMove(...))` action is -performed more than once (e.g. you write `... -.WillRepeatedly(Return(ByMove(...)));`)? Come think of it, after the first time -the action runs, the source value will be consumed (since it’s a move-only -value), so the next time around, there’s no value to move from -- you’ll get a -run-time error that `Return(ByMove(...))` can only be run once. +Quiz time! What do you think will happen if a `Return` action is performed more +than once (e.g. you write `... .WillRepeatedly(Return(std::move(...)));`)? Come +think of it, after the first time the action runs, the source value will be +consumed (since it’s a move-only value), so the next time around, there’s no +value to move from -- you’ll get a run-time error that `Return(std::move(...))` +can only be run once. If you need your mock method to do more than just moving a pre-defined value, remember that you can always use a lambda or a callable object, which can do @@ -2817,7 +2812,7 @@ pretty much anything you want: ``` Every time this `EXPECT_CALL` fires, a new `unique_ptr<Buzz>` will be created -and returned. You cannot do this with `Return(ByMove(...))`. +and returned. You cannot do this with `Return(std::make_unique<...>(...))`. That covers returning move-only values; but how do we work with methods accepting move-only arguments? The answer is that they work normally, although @@ -4298,7 +4293,7 @@ particular type than to dump the bytes. ### Mock std::function {#MockFunction} `std::function` is a general function type introduced in C++11. It is a -preferred way of passing callbacks to new interfaces. Functions are copiable, +preferred way of passing callbacks to new interfaces. Functions are copyable, and are not usually passed around by pointer, which makes them tricky to mock. But fear not - `MockFunction` can help you with that. |