c++ - 如何模拟对 boost::posix_time::microsec_time::local_time() 的调用?
问题描述
我在尝试测试 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;
解决方案
推荐阅读
- javascript - 将xpath放到侧边栏扩展html中的选定元素
- twitter-bootstrap - 如何在 Bootstrap 中制作文本标签常规字体(非粗体)?
- javascript - 如何在javascript中模拟构造函数调用
- c# - 直到我在 Asp.Net Core 3 中显式调用具有属性的 validate 方法后,IOptions 验证才会触发
- python - 如何按角度旋转python / numpy中的一维线图数组?
- python - 如何使图片从列表中出现并使其消失,同时在pygame中为其设置变量?
- ruby - VSCode/Rubocop 抱怨未配置的警察
- flutter - Flutter中的登录页面
- html - 两列不同宽度的图像。第一个是固定的,第二个应该缩小到剩余宽度
- android - 我应该取消android活动中的协程吗?