Dear Google Mock Group Member,
I have a question regarding ON_CALL marco,
Here is my sample code:
//////////////////////////////////////////////////////////////////////////
// Preparation
CCommandParserMocker commandParser;
ON_CALL( commandParser, GetCommand( _, _ ) )
.WillByDefault( Return( ERROR_INVALID_INDEX ) );
//////////////////////////////////////////////////////////////////////////
// Execution
int nReturnCode = ERROR_GENERAL;
CCommandHandler commandHandler( commandParser );
nReturnCode = commandHandler.ProcessCommand();
//////////////////////////////////////////////////////////////////////////
// Verification
EXPECT_EQ( ERROR_INVALID_INDEX, nReturnCode );
My code logic is CCommandHandler::ProcessCommand function will call CCommandParser::GetCommand function,
So I use GoogleMock ON_CALL marco to control CCommandParser::GetCommand function returns an error code.
However, every time when I run this test, I get the following result:
GMOCK WARNING:
Uninteresting mock function call - taking default action specified at:
d:\testing\unittest\cli\commandcontainertests.cpp:35:
Function call: GetCommandId(NULL, @0017FA35 4-byte object <6400 0000>)
Returns: -12132
Stack trace:
It seems Google Mock framework does not expect the CCommandParser::GetCommand function will be called, so it give me a warning.
Therefore, I do research on Google Mock website, I saw that I can use EXPECT_CALL with WillRepeatedly to resolve this issue,
So I change my code as follows and then everything is right
//////////////////////////////////////////////////////////////////////////
// Preparation
CCommandParserMocker commandParser;
EXPECT_CALL( commandParser, GetCommand( _, _ ) )
.WillRepeatedly( Return( ERROR_INVALID_INDEX ) );
//////////////////////////////////////////////////////////////////////////
// Execution
int nReturnCode = ERROR_GENERAL;
CCommandHandler commandHandler( commandParser );
nReturnCode = commandHandler.ProcessCommand();
//////////////////////////////////////////////////////////////////////////
// Verification
EXPECT_EQ( ERROR_INVALID_INDEX, nReturnCode );
However, in my understanding, I will use EXPECT_CALL when I need to verify the call interaction between two class,
In above case, I do not really care about whether CCommandParser::GetCommand function is called by CCommandHandler or not,
I just want it return an error code when it is called, I think in this situation, I should use ON_CALL better,
Because the readers of my testing program will know the focus on this testing is CCommandHandler::ProcessCommand, not CCommandParser::GetCommand.
Although I can use ON_CALL with NiceMock to avoid the warning of Google Mock, but I do not really want to use NiceMock,
Because I still hope I can get other warning from Google Mock.
My question is how can I use ON_CALL in this situation without warning message?
Also I am confusing it seems I can use EXPECT_CALL to replaces ON_CALL totally,
So why do you provide ON_CALL? and when do I use ON_CALL not EXPECT_CALL?
Thank you very much.
Best regards and have a nice day.
Tom Liu