我试图单元测试的HTTP API写在C++:惩戒常量值测试
void getLogNames(Request & req, Response & res)
{
vector<string> files = getFilesInDirectory(LOG_LOCATION, ".log", false);
json response(files);
res.send(response);
}
的问题是,LOG_LOCATION
从common.h
包括在内,是const
,并且不能由我的测试中被改变代码:
const std::string LOG_LOCATION = "/var/log"
我试图在文件上这样做:
#ifdef UNIT_TEST
#include <common_mock.h>
#else
#include <common.h>
#endif
然而,common.h
被列入了在被链接某些共享库,我将不得不UNIT_TEST
挂钩添加到所有这些文件并重新构建共享库以及,我宁愿避免...
是有更简单的方法,我可以做到这一点,一些#define
技巧或什么?
'#define LOG_LOCATION std :: string {“/ some/other/location”}'? – nwp
@nwp这仍然需要放在源文件的'UNIT_TEST'模拟头中,对吗? – RPGillespie
看来你想要测试'getLogNames':提供'getFilesInDirectory()'的模拟实现,并将其链接到测试。 –