I have a class Foo
that stores a pointer to a callback. The callback could be invoked with a method InvokeCallback()
.
void* SomeCallback(void* a) {
return (void*)(*(int*)a + 10);
}
class Foo {
public:
typedef void* (*CallbackFunction)(void*);
SetCallback(CallbackFunction callback) {
this->callback = callback;
}
InvokeCallback() {
callback(20);
}
private:
CallbackFunction callback;
}
How is it possible to test (using google test) that SomeCallback()
was invoked with a specific parameter (20 in the abovementioned case)?
EDIT:
I came up with a very ugly looking solution. The tests are passed, but the new error appeared at the end ERROR: 1 leaked mock object found at program exit
.
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));
}
Foo
in 1st place? Are you able to changeFoo
? – LinctusFoo
. – Uniaxial