sleep() call inside gmock's EXPECT_CALL
Since you said C++98 is preferable rather than compulsory, first I'll give a nice neat C++11 answer:
EXPECT_CALL(*_mock, Func(_,_,_)).Times(1)
.WillOnce(DoAll(InvokeWithoutArgs([TimeToSleep](){sleep(TimeToSleep);}),
Invoke(_mock, &M_MyMock::FuncHelper)));
Otherwise (for C++98), define a wrapper function elsewhere in the code:
void sleepForTime()
{
sleep(TimeToSleep);
}
And then:
EXPECT_CALL(*_mock, Func(_,_,_)).Times(1)
.WillOnce(DoAll(InvokeWithoutArgs(sleepForTime),
Invoke(_mock, &M_MyMock::FuncHelper)));
Note that here, TimeToSleep
will have to be a global variable.
EDIT: As per suggestion from OP in comments, if TimeToSleep
is a compile-time constant you can avoid the global variable:
template <int Duration>
void sleepForTime()
{
sleep(Duration);
}
...
EXPECT_CALL(*_mock, Func(_,_,_)).Times(1)
.WillOnce(DoAll(InvokeWithoutArgs(sleepForTime<TimeToSleep>),
Invoke(_mock, &M_MyMock::FuncHelper)));