How Do I Use ON_CALL Without Warning and When Do I Use ON_CALL Not EXPECT_CALL

8,038 views
Skip to first unread message

Chih-Chung Liu

unread,
Feb 15, 2011, 10:14:08 PM2/15/11
to googl...@googlegroups.com, liuc...@gmail.com
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

Martin Svensson

unread,
Feb 16, 2011, 2:15:38 AM2/16/11
to Chih-Chung Liu, googl...@googlegroups.com
I think this may be what you want to know:

ON_CALL().WillByDefault() is for NiceMock (only). It will always use defaults, but you can change the defaults.

EXPECT_CALL.WillRepeatedly() or EXPECT_CALL.Times(AtLeast(1)) is for StrictMock (only). You can add DoDefault() if you want.

/Martin Svensson

Martin Svensson

unread,
Feb 17, 2011, 2:22:09 AM2/17/11
to Chih-Chung Liu, Google C++ Mocking Framework
On 2011-02-17 07:01, Chih-Chung Liu wrote:
> Originally, I use ON_CALL and EXPECT_CALL to distinguish stubbing and
> mocking.
> When I just stub an object, I use ON_CALL, and mock an object with
> EXPECT_CALL.
> Also, I usually don't want to use NickMock or StrictMock,
> As you say, NickMock is too nice, also I think StrictMock is also too
> strict,
> Because sometimes warning message is enough, I don't want to case
> failed due to StrictMock with uninteresting called.
> However, as I mentioned before, if I use ON_CALL on normal mock, it
> will give me some warning about uninteresting, but I really don't want
> to see it also, because when I use ON_CALL, which means I just stub an
> object, I don't care about the interaction.
>
> So, I would like to ask your opinion,
> What is your suggestion that if I only need to stub an object, is
> NickMock with ON_CALL or normal mock with EXPECT_CALL?

Not sure if I understand correctly, but here goes:
You could use a NiceMock with EXPECT_CALL after all, but personally I
would do something like this:
(Haven't tried compiling it, though.)

class MyInterface
{
public:
virtual void f1() = 0;
virtual void f2() = 0;
};

class MyStub:
public NiceMock <MyInterface>
{
public:
MOCK_METHOD0(f1, void ()); // Use ON_CALL for this one
};

class MyMock:
public StrictMock <MyStub>
{
public:
MOCK_METHOD0(f2, void ()); // Use EXPECT_CALL for this one
};

/Martin

Martin Svensson

unread,
Feb 17, 2011, 2:49:03 AM2/17/11
to Chih-Chung Liu, Google C++ Mocking Framework
On 2011-02-17 08:40, Chih-Chung Liu wrote:
If my understanding is correct, your suggestion is

We should use NiceMock with ON_CALL for stub object.
We should use StrictMock with EXPECT_CALL for mock object.
So in your example, we can separate stub and mock from one interface.

Is it correct?

Yep, but it's ultimately you that decide how you want to use it.

/Martin

Reply all
Reply to author
Forward
0 new messages