GMock supports what they call hi-perf dependency injection for mocking non-virtual methods .
The bottom line is to use the templates:
template <class PacketStream> void CreateConnection(PacketStream* stream) { ... } template <class PacketStream> class PacketReader { public: void ReadPackets(PacketStream* stream, size_t packet_num); };
Then you can use CreateConnection () and PacketReader in the production code and use the CreateConnection () and PacketReader tests in the tests.
For C functions, they recommend interfaces, so probably not what you want. However, if you have separate libraries, you can always refer to the test library, which contains functions with the same signatures as the deployment library. You could even do it dynamically with LD_PRELOAD if you felt especially brave. That sounds like a lot of links to me.
Cxxtest , if you look in section 8.1, advanced functions support some macros to simplify the use / creation of interfaces:
From this link:
CXXTEST_MOCK_GLOBAL( time_t, time, ( time_t *t ), ( t ) ); 8.1.2. Mock Functions in Tested Code
Validated code uses macrobial global functions, rather than directly using global functions. You get access to the layout of functions in the T namespace (for the test), so the tested code calls T :: time () instead of time (). This is the equivalent of using abstract interfaces instead of concrete classes.
// rand_example.cpp
I was very lucky with the previous Cxxtest method.
source share