title: Using google test to check callbacks
body: <p>I have a class <code>Foo</code> that stores a pointer to a callback. The callback could be invoked with a method <code>InvokeCallback()</code>.</p>
  
  <pre><code>void* SomeCallback(void* a) {
  
    return (void*)(*(int*)a + 10);
  
  }
  
  
  class Foo {
  
  public: 
  
      typedef void* (*CallbackFunction)(void*);
  
      SetCallback(CallbackFunction callback) {
          this-&gt;callback = callback;
      }
  
      InvokeCallback() {
          callback(20);
      }
  
  private:
  
      CallbackFunction callback;
  
  }
  </code></pre>
  
  <p>How is it possible to test (using google test) that <code>SomeCallback()</code> was invoked with a specific parameter (20 in the abovementioned case)? </p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>I came up with a very ugly looking solution. The tests are passed, but the new error appeared at the end <code>ERROR: 1 leaked mock object found at program exit</code>.</p>
  
  <pre><code>class MockCallbackClass {
  public:
      MOCK_METHOD1(SomeCallback, void*(void*));
  };
  
  MockCallbackClass mock; // Nasty global variable
  
  void* __SomeOtherCallback(void* a) {
  
      return mock.SomeCallback(a);
  
  }
  
  TEST(Callback, MockCallback) {
  
      Foo foo;
      foo.SetCallback(__SomeOtherCallback);
      EXPECT_CALL(mock, SomeCallback((void*)10)).WillOnce(testing::Return((void*)20));
  
  }
  </code></pre>