gmock


how to set custom ref-variable in gmock


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 XXXClient mock class, let's name it XXXClientMock as following:
class XXXClientMock : public XXXClient
{
public:
MOCK_METHOD2(QueryXXX, QueryResult (Request&, Response&));
};
Then, use GMock Action SetArgReferee to set the custom parameter, as following:
TEST(XXXRunnerTC, SetArgRefereeDemo)
{
XXXCLientMock oMock;
// set the custom response object
Response oRsp;
oRsp.attr1 = “…”;
oRsp.attr2 = “any thing you like”;
// associate the oRsp with mock object QueryXXX function
EXPECT_CALL(oMock, QueryXXX(_, _)).
WillOnce(SetArgReferee<1>(oRsp));
// OK all done
// call QueryXXX
XXXRunner oRunner;
QueryResult oRst = oRunner.DoSomething(oMock);
…
// use assertions to verity your expectation
EXPECT_EQ(“abcdefg”, oRst.attr1);
……
}
Summary
GMock provide a series of actions to make it convenient to mock functions, such as SetArgReferee for reference or value, SetArgPointee for pointer, Return for return, Invoke for invoke custom mock function (with simple test logic), you can see here for more details.
Enjoy it :)
Thank you
Check out the SetArgReferee in the Google Mock cheat sheet.

Related Links

gmock ReturnRef returns compilation error
How to call function pointer which is passed to Mock method?
In gmock, Is there a way to have mock object return an instance of an user defined object?
GMock testing destructor calls
set EXPECT_CALL to redirect the call to the original method
Capture GMOCK string parameter
Mocking non-virtual method generating errors
gmock matcher doesn't match my arguments by reference
What did you do to solve gmock you mentioned (link enclosed)?
how to set custom ref-variable in gmock

Categories

HOME
cygwin
grok
eslint
socket.io
edge
rom
echo
lanczos
sendgrid-api-v3
32bit-64bit
datastage
php-5.6
supervisor
endpoint
enterprise-architect
atlassian
docx
mongodb-csharp
git-extensions
soci
nodemcu
poedit
imagemap
ios10.2
flask-sqlalchemy
jquery-callback
froala
opentk
jquery-multiselect
jackson-modules
8051
tpm
alm
dotspatial
volatile
kudu
android-sugarorm
yowsup
servicestack-text
salesforce-communities
git-rewrite-history
nohup
password-protection
railstutorial.org
css-position
cin
ogc
magento-1.9.3
parser-generator
chronicle-map
oim
jboss-4.2.x
textsum
openal
magento-2.0.7
chai-as-promised
yii2-api
datamaps
posixct
filehandle
filenet
testrail
autosar
tinkerpop3
greatest-n-per-group
scaffolding
nservicebus5
stream-processing
realsense
sapi
rewrite
rubaxa-sortable
sapscript
bufferedinputstream
nodelist
angular-gettext
boost-program-options
browserify-shim
sip-server
crowd
metatrader5
cbind
wcm
malformedurlexception
synapse
clickbank
qscrollarea
gd-graph
embedded-fonts
kendo-dataviz
neos-server
cmmi
twisted.web
correlated-subquery
josql
httponly
infobright
landscape-portrait
webdev.webserver
load-time
instantiationexception
jqueryform
pos-for-.net
request-headers
mathematical-notation
escrow
idatareader

Resources

Encrypt Message