How to group some expectations to reuse them and know failure call stack?

1,680 views
Skip to first unread message

lxu4net

unread,
Oct 22, 2010, 12:23:32 AM10/22/10
to Google C++ Mocking Framework
I have some expectations they always be used at same time. So I want
to group them for readable.
I put them in a subroutine like I do it at Google Test. Then I use
SCOPED_TRACE macro to tell which invocation of the sub-routine the
failure is from. But it don't work, I think the reason is the Google
Mock check the expectations at destructor.
So the problem is how to group some expectations to reuse them and
know failure call stack.

Vlad Losev

unread,
Oct 22, 2010, 1:12:52 AM10/22/10
to lxu4net, Google C++ Mocking Framework
Hi,

You can set expectations in a function if you pass your mock there as a pointer or reference:

void SetExpectationOnMyMock(MyMockClass* mock) {
  EXPECT_CALL(*mock, MyMethod(_, _)).WIllOnce(Return(true));
  EXPECT_CALL(*mock, MyAnotherMethod(_)).WIllOnce(Return(5));
}

Don't forget, you have to set all the expectations on the mock before you can make calls to it. Also, SCOPED_TRACE will not help you figure where an expectation has been set; only where it has been violated.
HTH,
Vlad

lxu4net

unread,
Oct 22, 2010, 2:00:36 AM10/22/10
to Google C++ Mocking Framework
Thanks Vlad. You are right. SCOPED_TRACE can figure where it has been
violated.

I made the mistake that put it at InSequence scope:
TEST(TaskCaseName, testName)
{
MyMockClass mock;

{
SCOPED_TRACE("");
InSequence dummy;

SetExpectationOnMyMock(mock, true);
SetExpectationOnMyMock(mock, false);
}

Loader loader(mockr);
ASSERT_EQ(0, loader.checkAndLoad(now));
}

After move it to the begin of test method, it's working:
void SetExpectationOnMyMock(MyMockClass & mock, bool expectParam1) {
EXPECT_CALL(mock, MyMethod(_, expectParam1)).WIllOnce(Return(true));
EXPECT_CALL(mock, MyAnotherMethod(_)).WIllOnce(Return(5));
}
TEST(TaskCaseName, testName)
{
SCOPED_TRACE("");
MyMockClass mock;

{
InSequence dummy;

SetExpectationOnMyMock(mock, true);
SetExpectationOnMyMock(mock, false);
}

Loader loader(mockr);
ASSERT_EQ(0, loader.checkAndLoad(now));
}


SCOPED_TRACE can't give me more information about where an expectation
has been set. But it's I want to get.
Like this example, I call expectNoLoad twice with different
parameters. There are three expectations in expectNoLoad method. I
can't easily find which expectNoLoad method was violated.

Maybe I need write a macro instead of function but it's not very C++.
Is there better solution?

On 10月22日, 下午1时12分, Vlad Losev <v...@losev.com> wrote:
> Hi,
>
> You can set expectations in a function if you pass your mock there as a
> pointer or reference:
>
> void SetExpectationOnMyMock(MyMockClass* mock) {
>   EXPECT_CALL(*mock, MyMethod(_, _)).WIllOnce(Return(true));
>   EXPECT_CALL(*mock, MyAnotherMethod(_)).WIllOnce(Return(5));
>
> }
>
> Don't forget, you have to set *all* the expectations on the mock before you
> can make calls to it. Also, SCOPED_TRACE will not help you figure where an
> expectation has been set; only where it has been violated.
>

Zhanyong Wan (λx.x x)

unread,
Oct 22, 2010, 12:59:09 PM10/22/10
to lxu4net, Google C++ Mocking Framework

http://code.google.com/p/googlemock/wiki/CookBook#Controlling_How_Much_Information_Google_Mock_Prints

You can use the --gmock_verbose=info flag to see what expectations are set.

--
Zhanyong

lxu4net

unread,
Oct 22, 2010, 10:21:53 PM10/22/10
to Google C++ Mocking Framework
I have tried "--gmock_verbose=info". It tell me not where the failed
expectation was set but what expectations are set.

Maybe we need some help macros to save original __FILE__ and __LINE__,
then display them at failure message. Or add "Message()" after
EXPECT_CALL() to set extra failure message.

Example:
void SetExpectationOnMyMock(MyMockClass & mock, bool expectParam1,
string extraMessage) {
EXPECT_CALL(mock, MyMethod(_,
expectParam1)).WIllOnce(Return(true)).Message(extraMessage);
EXPECT_CALL(mock,
MyAnotherMethod(_)).WIllOnce(Return(5)).Message(extraMessage);
}
TEST(TaskCaseName, testName)
{
SCOPED_TRACE("");
MyMockClass mock;

{
InSequence dummy;

SetExpectationOnMyMock(mock, true, "The First exceptions");
SetExpectationOnMyMock(mock, false, "The Second exceptions");
}

Loader loader(mockr);
ASSERT_EQ(0, loader.checkAndLoad(now));
}
In failure message, we get the extra message:
Message: The Second exceptions


On 10月23日, 上午12时59分, Zhanyong Wan (λx.x x) <w...@google.com> wrote:
> http://code.google.com/p/googlemock/wiki/CookBook#Controlling_How_Muc...
Reply all
Reply to author
Forward
0 new messages