title: Google Mock Return a live element using ON_CALL
body: <p>I read through <a href="https://stackoverflow.com/questions/5144623/google-mock-return-a-list-of-values">Google Mock: Return() a list of values</a> and found out how to return a single element from a vector on each EXPECT_CALL, as such I wrote the following code which works:</p>
  
  <pre><code>    {
        testing::InSequence s1;
          for (auto anElem:myVecCollection) {
            EXPECT_CALL(myMockInstance, execute())
                .WillOnce(testing::Return(anElem));
          }
      }
  </code></pre>
  
  <p>so far so good...
  Now I read not to use EXPECT_CALL unless you need to. <a href="https://groups.google.com/forum/#!topic/googlemock/pRyZwyWmrRE" rel="nofollow noreferrer">https://groups.google.com/forum/#!topic/googlemock/pRyZwyWmrRE</a>
  My use case, myMockInstance is really a stub providing data to the SUT(software under test).</p>
  
  <p>However, a simple EXPECT_CALL to ON_CALL replacement will not work(??), since ON_CALL with WillByDefault only calculates the return type only once(??)</p>
  
  <p>As such I tried setting up an ACTION. </p>
  
  <pre><code>ACTION_P(IncrementAndReturnPointee, p)
  {
      return (p)++;
  }
  
  ON_CALL(myMockInstance, execute())
          .WillByDefault(testing::Return
           (*(IncrementAndReturnPointee(myVecCollection.cbegin()))));
  </code></pre>
  
  <p>Clang gives </p>
  
  <pre><code>error: expected expression   'ACTION_P(IncrementAndReturnPointee, p)'
  </code></pre>
  
  <p>Then I tried setting up a functor and use the Invoke method on it.      </p>
  
  <pre><code>    struct Funct
      {
        Funct() : i(0){}
  
        myClass mockFunc(std::vector&lt;myClass&gt;  &amp;aVecOfMyclass)
        {
          return aVecOfMyclass[i++];
        }
  
        int i;
      };
  
      Funct functor;                
      ON_CALL(myMockInstance, execute())
          .WillByDefault(testing::Return(testing::Invoke(&amp;functor, functor.mockFunc(myVecCollection))));
  </code></pre>
  
  <p>Clang gives </p>
  
  <pre><code>no matching function for call to 'ImplicitCast_'
          : value_(::testing::internal::ImplicitCast_&lt;Result&gt;(value)) {}
  </code></pre>
  
  <p>Now , I am fairly new to google-mock but have used google-test extensively. 
  I am a bit lost with the Google-Mock doc. I wanted to know, whether I am on the right path, in terms of what I needed. 
  If one of you could point to me , which approach is the correct one; or whether I am even close to the right approach, I can take it from there and debug the "close to right approach" further. 
  Thanks</p>