vendredi 22 septembre 2017

Mocking const values for testing

I am trying to unit test an HTTP API written in C++:

void getLogNames(Request & req, Response & res) 
{
    vector<string> files = getFilesInDirectory(LOG_LOCATION, ".log", false);
    json response(files);
    res.send(response);
}

The problem is that LOG_LOCATION is included from common.h and is const, and can't be changed by my testing code:

const std::string LOG_LOCATION = "/var/log"

I've tried doing this at the top of the file:

#ifdef UNIT_TEST
#include <common_mock.h>
#else
#include <common.h>
#endif

However, common.h is included in some shared libraries that are being linked in, and I would have to add UNIT_TEST hooks to all those files and rebuild the shared libraries as well, which I would rather avoid...

Is there an easier way I could be doing this, some #define tricks or something?

Aucun commentaire:

Enregistrer un commentaire