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.
>