I have developed Mockingbird a mocking framework for c++, it depends on function injection, and here is an example on how it works:
Assume you have the following class Foo
and you need to mock:
struct MyStruct{
int x, y;
};
class Foo{
public:
virtual const MyStruct CreateMyStruct(int x, int y) { return MyStruct{ x,y }; }
};
Then you write a testing fixture only once for the whole project:
const MyStruct CreateMyStructDummy(int x, int y) { return MyStruct{0,0}; }
START_MOCK(FooMock, Foo)
FUNCTION(CreateMyStruct, const MyStruct, (int x, int y), &CreateMyStructDummy, x, y)
END_MOCK(FooMock)
Then in the tests you write the desired substitute for example:
const MyStruct CreateMyStructSubstitute(int x, int y) { return MyStruct{ x + 10, y + 10 }; }
and inject it like:
FooMock fooMock;
fooMock.InjectCreateMyStruct(CreateMyStructSubstitute); // Mocking function injection.
auto created = fooMock.CreateMyStruct(5,5);
EXPECT_EQ(created.x, 15);
EXPECT_EQ(created.y, 15);
The code is in the file Mockingbird.hpp
and totally depends on macros, it is short and straightforward, I ask for reviewing the code on github here, thanks.