title: Google Mock - how do I return a different value using EXPECT_CALL to exit a loop
body: <p>Using google mock, how do I specify an EXPECT_CALL with a return value N times, and then a different value N+1? </p>
  
  <p>The only way I can get my test to pass is if I manually specify each iteration e.g.</p>
  
  <pre><code>EXPECT_CALL(mock, Read(address)).Times(5)
  .WillOnce(Return(0))
  .WillOnce(Return(0))
  .WillOnce(Return(0))
  .WillOnce(Return(0))
  .WillOnce(Return(1));
  </code></pre>
  
  <p><strong>Test Description</strong></p>
  
  <p>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. </p>
  
  <p>Unfortunately the code in question is on a separate machine but here's a representative example.</p>
  
  <pre><code>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) &amp;&amp; (readIndex &lt; maxData))
  {
      // Read data word.
      dataBuffer[readIndex++] = Read(dataAddress);
  
      // Read status to determine if more data is available.
      dataIsAvailable = Read(statusAddress);
  }
  </code></pre>
  
  <p>The Read method is mocked.</p>
  
  <p>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.</p>
  
  <pre><code>EXPECT_CALL(mock, Read(address)).Times(4).WillRepeatedly(Return(1));
  EXPECT_CALL(mock, Read(address)).Times(1).WillOnce(Return(0));
  </code></pre>
  
  <pre><code>Expected: to be called 4 times
   Actual: never called - unsatisfied and active
  </code></pre>
  
  <p>As there is no method...</p>
  
  <pre><code>WillN(Return(1), 4)
  </code></pre>
  
  <p>... I presume there is a less fixed way of structuring an EXPECT_CALL for more complex and/or longer sequences of data?</p>