我有一些代码:
boost::system::error_code clock_error;
const boost::chrono::steady_clock::time_point START =
boost::chrono::steady_clock::now(clock_error);
我想模拟 now() 函数,所以我的测试代码包含以下内容:
boost::chrono::steady_clock::time_point start(boost::chrono::seconds(0));
boost::system::system_error clock_error(1, BOOST_CHRONO_SYSTEM_CATEGORY, "foo");
mocks.ExpectCallFuncOverload( (boost::chrono::steady_clock::time_point (boost::chrono::steady_clock::*) (boost::system::error_code& clock_error))
&boost::chrono::steady_clock::now)
.With(Out(clock_error))
.Return(start);
但这不能编译。我收到此错误:
错误:没有匹配将函数“now”转换为类型 ‘boost::chrono::steady_clock::time_point (类 boost::chrono::steady_clock::)(class boost::system::error_code&) {又名 类 boost::chrono::time_pointboost::chrono::steady_clock (类 boost::chrono::steady_clock::)(class boost::system::error_code&)}’ 模拟。ExpectCallFuncOverload(
包含在...的文件中
注意:候选者是: static boost::chrono::steady_clock::time_point boost::chrono::steady_clock::now(boost::system::error_code&)
static BOOST_CHRONO_INLINE time_point now(system::error_code & ec); ^
../../../KACotsCode/boost/boost/chrono/system_clocks.hpp:162:46:注意: 静态升压::chrono::steady_clock::time_point 升压::计时::steady_clock::now() 静态 BOOST_CHRONO_INLINE time_point now() BOOST_NOEXCEPT;
^
有人能发现问题吗?
我看了看HippoMocks。那东西科技含量很高。我可能不会相信它的生产代码,因为它非常具有侵入性并且完全没有文档记录。
但是,它确实在 GCC 上做了一些令人印象深刻的事情 - 从代码来看 - MSVC:
#define BOOST_CHRONO_HEADER_ONLY
#include "https://raw.githubusercontent.com/dascandy/hippomocks/master/HippoMocks/hippomocks.h"
#include <boost/chrono.hpp>
#include <iostream>
namespace chrono = boost::chrono;
using Clock = chrono::steady_clock;
int main() {
MockRepository mocks;
boost::system::error_code clock_error = make_error_code(boost::system::errc::file_exists);
mocks
.ExpectCallFunc(Clock::now) //
.With(Out(clock_error)) //
.Return(Clock::time_point::max());
auto START = Clock::now(clock_error);
std::cout //
<< clock_error.message() << ": " //
<< std::hex << START.time_since_epoch().count() << std::endl;
}
打印预期内容
File exists: 7fffffffffffffff
奖励:时间旅行者版
#define BOOST_CHRONO_HEADER_ONLY
#include "https://raw.githubusercontent.com/dascandy/hippomocks/master/HippoMocks/hippomocks.h"
#include <boost/chrono.hpp>
#include <iostream>
namespace chrono = boost::chrono;
using Clock = chrono::steady_clock;
int main() {
std::cout << "Boost version: " << BOOST_VERSION << std::endl;
std::cout << "GCC version: " << __VERSION__ << std::endl;
MockRepository mocks;
boost::system::error_code clock_error = make_error_code(boost::system::errc::file_exists);
mocks
.ExpectCallFunc(static_cast<Clock::time_point (&)(boost::system::error_code&)>(Clock::now)) //
.With(Out(clock_error)) //
.Return(Clock::time_point::max());
auto START = Clock::now(clock_error);
std::cout //
<< clock_error.message() << ": " //
<< std::hex << START.time_since_epoch().count() << std::endl;
}
正如您正确猜测的那样,您必须显式强制重载函数类型。更重要的是,HippoMocks 不喜欢在这里传递函数指针,所以一定要区分函数 reference:static_cast<Clock::time_point (&)(boost::system::error_code&)>(Clock::now)
“不幸的是,简洁的文档限制了某些开发人员的访问并阻碍了进展” [原文如此]
² Compiler Explorer 无法执行代码,因为