我在尝试测试 cpp 类的某些功能时遇到了一些问题。我正在测试我的类,我们称之为 myManager,它有一个名为 generateActionCommand 的公共方法,它使用 boost posix_time 函数 local_time() 返回一个 ptime 结构,以便生成格式为“timestamp,id,CMD_TYPE”的命令,其中:
- timestamp 是一个字符串,表示机器的时间为 hh:mm:ss.ms;
- id 是一个整数
- CMD_TYPE,是一个字符串,例如“START”
现在,我正在使用 boost.test 和 turtle-mock 测试 myManager 并且我已经编写了一些单元测试并使用模拟来模拟其他类,但是我如何测试时间戳是否正确生成?(我可以测试函数尊重格式,使用正则表达式,但我不能保证返回的时间。我想使用 MOCK_FUNCTION 模拟函数,但它似乎不起作用。
下面是我要测试的方法的代码:
std::string OEManager::generateActionCommand(const char* cmd_type) {
std::string ret_msg;
pt::ptime current_time = pt::microsec_clock::local_time();
char timestamp[13];
//NOTE: NO us(microseconds) support on windows machine time in format (hh:mm:ss.ms)
snprintf(timestamp, 13, "%02d:%02d:%02d.%03d",
current_time.time_of_day().hours(), current_time.time_of_day().minutes(),
current_time.time_of_day().seconds(), current_time.time_of_day().fractional_seconds());
ret_msg = timestamp;
ret_msg.append ("," + std::to_string(sequence_id) + "," + cmd_type);
return ret_msg;