Did anyone ever hear of a UT library that utilizes hooks to perform the mocks?
For comparison, you have gMock today, which works great. However, to use gMock, you must design your code in a “testable” way – i.e. factories everywhere, interfaces (even if there’s only one clear implementation…), and more inconveniences.
I’m imagining a library very similar to gMock, which instead of utilizing inheritance & polymorphism to create mocks, utilizes hooking a function or a member function, with the necessary mock logic, thus saving you the hassle of complicated your code with unnecessary interfaces.
For example, this is what i would imagine:
class A {
public:
A(int val) : m_val(val) {}
int doSomething(int x) {
return x + m_val;
}
private:
int m_val;
};
TEST(Sanity, TestSanity) {
auto mock = MOCK(A::doSomething); // Hook the method
mock.willInstead([](int x) { return x - 5; }); // Invoke this logic in the hook
A test(500);
ASSERT_EQ(a.doSomething(1), 495);
}