Google Mock functions changing value of parameter
Asked Answered
A

2

7

I am trying to mock out some code that returns information by writing to one of the references passed to the function as a parameter. Is there a good way to mock this behavior such that I can have the test code determine what value is written to that variable?

Autecology answered 2/10, 2015 at 16:8 Comment(2)
ON_CALL - WillByDefault - Invoke, and delegate?Santos
groups.google.com/forum/embed/#!topic/googlemock/dtM0M_cTJik if this is what you are referring toPlotter
A
8

I did a little research and it turns out that GMock has a couple of nice options. From the Google Mock Cheat Sheet

SetArgReferee<N>(value)
SetArgPointee<N>(value)

"Assign value to the variable referenced by the N-th (0-based) argument" and "Assign value to the variable pointed by the N-th (0-based) argument" respectively.

Autecology answered 2/10, 2015 at 16:25 Comment(1)
Updated link to cheat sheet: google.github.io/googletest/gmock_cheat_sheet.htmlTinct
O
0

You have quite a few options. SetArgReferee(value) and SetArgPointee(value), as Daniel pointed out in 2015.

Then there's SetArrayArgument(first, last) for arrays.

Or you can use Invoke capabilities to invoke your own functions and do what you want: Invoke, InvokeWithoutArgs, InvokeWithoutArgs, InvokeArgument.

Read their descriptions in the Side Effects and Invoke sections on the Googlemock's cheat sheet page.

Ogilvy answered 10/2, 2021 at 10:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.