I know the first part of this question has been asked before, but that was a long time ago :). I was wondering if the mean time any of the open source mocking frameworks caught up with Typemock Isolator++ when it comes to mocking non-virtual methods and C functions. I’m mostly interested in gcc under Linux. So far, I’m interested in mocking accessors (so that I can simulate states in the mocked object – see below) and replacing C functions from other libraries (select, pcap_*, etc.).
class Foo {
public:
...
bool IsCondition() { return condition; };
...
private:
bool condition;
}
// I want a framework that allows me to do something like this:
TEST(TestFoo) {
MOCK_INTERFACE(Foo) mock_foo;
EXPECT_CALL(mock_foo, IsCondition).returns(true);
EXPECT(mock_foo.IsCondition());
}
GMock supports what they call hi-perf dependency injection for mocking non-virtual methods.
The gist, from the link above, is to use templates:
For C functions they recommend interfaces, so probably not what you want. However, if you have separate libraries you could always link against a testing library, which contains functions with the same signatures as the deployment library. You could even do it dynamically with LD_PRELOAD if you were feeling particularly daring. That sounds like a lot of linking to me.
Cxxtest, if you look under section 8.1 in advanced features supports some macros to make using/creating interfaces easier:
From that link:
I’ve had good luck with the Cxxtest approach in the past.