title: Google Test WillOnce(Return( )) manipulates the expected return-value
body: <p>I am currently trying to test the capabilities of google test in c++. (To be more specific: google mock)</p>
  
  <p>Now I ran into a problem. It should be super easy, it worked before, but somehow it got messed up and the framework isn't working as expected. Enough talk, here is my problem.</p>
  
  <p>I try to do the simple thing to see if the function "DoSomeMathTurtle" gets invoked once and returns the expected value. But ".WillOnce(Return(x))" manipulates the expected value that it is always true.</p>
  
  <pre><code>class Turtle {
      ...
      virtual int DoSomeMathTurtle(int , int); //Adds two ints together and returns them
      ...
  };
  </code></pre>
  
  <p>My mocking class:</p>
  
  <pre><code>    class MockTurtle : public Turtle{
      public:
          MockTurtle(){
              ON_CALL(*this, DoSomeMathTurtle(_, _))
                  .WillByDefault(Invoke(&amp;real_, Turtle::DoSomeMathTurtle)); 
              //I did this so the function gets redirected to a real
              // object so those two numbers actually get added together 
              //(without that it returns always 0)
  
          }
          MOCK_METHOD2(DoSomeMathTurtle, int(int, int));
      private:
          Turtle real_;
      };
  </code></pre>
  
  <p>Basic Testing Class:</p>
  
  <pre><code>class TestTurtle : public ::testing::Test{
  protected:
      //De- &amp; Constructor
      TestTurtle(){}
      virtual ~TestTurtle(){} //Has to be virtual or all tests will fail
  
      //Code executed before and after every test
      virtual void SetUp(){}
      virtual void TearDown(){}
  
      //Test Fixtures which can be used in each test
      MockTurtle m_turtle;
   };
  </code></pre>
  
  <p>My Testcase:</p>
  
  <pre><code>    TEST_F(TestTurtle, MathTest){
              int x = 2; // Expected value
              int rvalue; // Returned value of the function
  
              EXPECT_CALL(m_turtle, DoSomeMathTurtle(_, _))
                  .Times(1)
                  .WillOnce(Return(x));
  
                  rvalue = m_turtle.DoSomeMathTurtle(3,3); // rvalue should be 6 but it is 2
                  cout &lt;&lt; "[          ] Expected value: " &lt;&lt; x &lt;&lt; " Returned value " &lt;&lt; rvalue &lt;&lt; endl;
                  //This prints: [          ] Expected value 2 Returned value 2
                  //Then the test passes !?
              }
          }
  </code></pre>
  
  <p>When I comment the <code>"WillOnce(Return(x))"</code> out, rvalue becomes 6 (the value it should become). When <code>"WillOnce(Return(x))"</code> &amp; </p>
  
  <pre><code>ON_CALL(*this, DoSomeMathTurtle(_, _))
                  .WillByDefault(Invoke(&amp;real_, Turtle::DoSomeMathTurtle)); 
  </code></pre>
  
  <p>(in the constructor of my mock-class) is commented out, rvalue becomes 0 (that should also happen).</p>
  
  <p>So as soon as <code>"WillOnce(Return(x))"</code> is in the game, rvalue is always <code>"x"</code>.</p>
  
  <p>Thanks in advance for your help!
  Have a nice, bug-free day!</p>