Avoid matching .WillOnce multiple times in Google Mock

UXkQEZ7 picture UXkQEZ7 · Aug 7, 2013 · Viewed 26.2k times · Source

I have a mock object setup that looks like this:

MyObject obj;
EXPECT_CALL(obj, myFunction(_))
.WillOnce(Return(1))
.WillOnce(Return(1))
.WillOnce(Return(1))
.WillRepeatedly(Return(-1));

Is there a way to not have to repeat .WillOnce(Return(1)) three times?

Answer

VladLosev picture VladLosev · Aug 8, 2013
using testing::InSequence;

MyObject obj;

{
  InSequence s;
  EXPECT_CALL(obj, myFunction(_))
      .Times(3)
      .WillRepeatedly(Return(1));
  EXPECT_CALL(obj, myFunction(_))
      .WillRepeatedly(Return(-1));
}