I have a test and I am setting expectation as follows:
EXPECT_CALL(*iter, NextBatch(_, _))
.WillOnce(Invoke(MakeMockNextBatchCb(
responses[0] /* values */, batch_size, at_end)))
.WillOnce(Invoke(MakeMockNextBatchCb(
responses[1] /* values */, batch_size, at_end)))
.WillOnce(Invoke(MakeMockNextBatchCb(
responses[2] /* values */, batch_size, at_end)));
It works and the test passes. In the above, the response is a vector.
However, I want to write parameterized tests and would have to convert this to some dynamic version. I have tried a few options, but none work. Setting expectations in a for loop would not work and I now understand why.
for(int i=0; i<response.size(); ++i) {
EXPECT_CALL(*iter, NextBatch(_, _))
.WillOnce(Invoke(MakeMockNextBatchCb(
responses[i] /* values */, batch_size, at_end)));
}
But I want an implementation similar to the above loop with functionality of the first example. Can someone please suggest how can I set expectation dynamically?
The expectations in gMock are "sticky" by default, they remain active even after their invocation upper bounds are reached. Next, gMock searches the expectations in the reverse order they are defined. As a result, only the last expectation will be used in your example with the loop.
To achieve the same behavior using the loop, it is necessary to set expectations in the reverse order and explicitly say that the expectations are not sticky: