Google Mock: Return() a list of values

Jonathan picture Jonathan · Feb 28, 2011 · Viewed 8.4k times · Source

Via Google Mock's Return() you can return what value will be returned once a mocked function is called. However, if a certain function is expected to be called many times, and each time you would like it to return a different predefined value.

For example:

EXPECT_CALL(mocked_object, aCertainFunction (_,_))
    .Times(200);

How do you make aCertainFunction each time return an incrementing integer?

Answer

Jonathan picture Jonathan · Feb 28, 2011

Use sequences:

using ::testing::Sequence;

Sequence s1;
for (int i=1; i<=20; i++) {
    EXPECT_CALL(mocked_object, aCertainFunction (_,_))
        .InSequence(s1)
        .WillOnce(Return(i));
}