首页 > 解决方案 > 如何模拟对 boost::posix_time::microsec_time::local_time() 的调用?

问题描述

我在尝试测试 cpp 类的某些功能时遇到了一些问题。我正在测试我的类,我们称之为 myManager,它有一个名为 generateActionCommand 的公共方法,它使用 boost posix_time 函数 local_time() 返回一个 ptime 结构,以便生成格式为“timestamp,id,CMD_TYPE”的命令,其中:

现在,我正在使用 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;

标签: c++boostturtle-mock

解决方案


推荐阅读