Mock implementations in C++
- by forneo
Hi guys,
I need a mock implementation of a class - for testing purposes - and I'm wondering how I should best go about doing that. I can think of two general ways:
Create an interface that contains all public functions of the class as pure virtual functions, then create a mock class by deriving from it.
Mark all functions (well, at least all that are to be mocked) as virtual.
I'm used to doing it the first way in Java, and it's quite common too (probably since they have a dedicated interface type). But I've hardly ever seen such interface-heavy designs in C++, thus I'm wondering.
The second way will probably work, but I can't help but think of it as kind of ugly. Is anybody doing that?
If I follow the first way, I need some naming assistance. I have an audio system that is responsible for loading sound files and playing the loaded tracks. I'm using OpenAL for that, thus I've called the interface "Audio" and the implementation "OpenALAudio". However, this implies that all OpenAL-specific code has to go into that class, which feels kind of limiting. An alternative would be to leave the class' name "Audio" and find a different one for the interface, e.g. "AudioInterface" or "IAudio". Which would you suggest, and why?