I am using gmock in my project and I meet a problem to set a custom reference variable for a mock function.
Suppose I have a class as following:
class XXXClient {
public:
void QueryXXX(const Request&, Response&);
};
class XXXRunner {
public:
void DoSomething(XXXClient&);
};
There is a Client Class XXXRunner::DoSomething using XXXClient::QueryXXX, and I Want to mock XXXClient to test XXXRunner::DoSomething.
The problem occurs that the second parameter of QueryXXX , that is ‘Response’, is not a return value, but a reference variable, which I fill some data into Response in XXXClient::QueryXXX. I want to set a custom data for the Response to verify different condition of XXXRunner::DoSomething.
The gmock framework can set expected returned value, but I cannot not find a way to set the "returned variable" ?
So How to do so?
First, make a
XXXClientmock class, let’s name itXXXClientMockas following:Then, use GMock Action
SetArgRefereeto set the custom parameter, as following:Summary
GMock provide a series of actions to make it convenient to mock functions, such as
SetArgRefereefor reference or value,SetArgPointeefor pointer,Returnfor return,Invokefor invoke custom mock function (with simple test logic), you can see here for more details.Enjoy it 🙂
Thank you