Using google mock, how do I specify an EXPECT_CALL with a return value N times, and then a different value N+1?
The only way I can get my test to pass is if I manually specify each iteration e.g.
EXPECT_CALL(mock, Read(address)).Times(5)
.WillOnce(Return(0))
.WillOnce(Return(0))
.WillOnce(Return(0))
.WillOnce(Return(0))
.WillOnce(Return(1));
Test Description
The code under test uses a status word to determine if a loop reading data should exit. EXPECT_CALL should set an expectation to return 1 from the mocked method Read() N times, and on the Nth cycle return 0 to indicate no more data.
Unfortunately the code in question is on a separate machine but here's a representative example.
const unsigned int statusAddress = 0;
const unsigned int dataAddress = 1;
const unsigned int maxData = 8;
unsigned int dataBuffer[maxData] = {0};
int readIndex = 0;
// Check if data is available (read returns 1).
unsigned int dataIsAvailable = Read(statusAddress);
// Keep reading data until no more is available or the buffer is full.
while ((dataIsAvailable == 1) && (readIndex < maxData))
{
// Read data word.
dataBuffer[readIndex++] = Read(dataAddress);
// Read status to determine if more data is available.
dataIsAvailable = Read(statusAddress);
}
The Read method is mocked.
If I add expectations in order, the latter EXPECT_CALL is used ( I presume the WillOnce overwrites WillRepeatedly in google code ). Obviously Im misunderstanding how multiple EXPECT_CALLs is ordered.
EXPECT_CALL(mock, Read(address)).Times(4).WillRepeatedly(Return(1));
EXPECT_CALL(mock, Read(address)).Times(1).WillOnce(Return(0));
Expected: to be called 4 times
Actual: never called - unsatisfied and active
As there is no method...
WillN(Return(1), 4)
... I presume there is a less fixed way of structuring an EXPECT_CALL for more complex and/or longer sequences of data?