Mocking with Boost::Test
- by Billy ONeal
Hello everyone :)
I'm using the Boost::Test library for unit testing, and I've in general been hacking up my own mocking solutions that look something like this:
//In header for clients
struct RealFindFirstFile
{
static HANDLE FindFirst(LPCWSTR lpFileName, LPWIN32_FIND_DATAW lpFindFileData) {
return FindFirstFile(lpFileName, lpFindFileData);
};
};
template <typename FirstFile_T = RealFindFirstFile>
class DirectoryIterator {
//.. Implementation
}
//In unit tests (cpp)
#define THE_ANSWER_TO_LIFE_THE_UNIVERSE_AND_EVERYTHING 42
struct FakeFindFirstFile
{
static HANDLE FindFirst(LPCWSTR lpFileName, LPWIN32_FIND_DATAW lpFindFileData) {
return THE_ANSWER_TO_LIFE_THE_UNIVERSE_AND_EVERYTHING;
};
};
BOOST_AUTO_TEST_CASE( MyTest )
{
DirectoryIterator<FakeFindFirstFile> LookMaImMocked;
//Test
}
I've grown frustrated with this because it requires that I implement almost everything as a template, and it is a lot of boilerplate code to achieve what I'm looking for.
Is there a good method of mocking up code using Boost::Test over my Ad-hoc method?
I've seen several people recommend Google Mock, but it requires a lot of ugly hacks if your functions are not virtual, which I would like to avoid.
Oh: One last thing. I don't need assertions that a particular piece of code was called. I simply need to be able to inject data that would normally be returned by Windows API functions.