我正在使用Catch2编写单元测试。
我想做的一件事是确保我捕捉到了正确的异常。在许多情况下我都会抛出相同的异常,因此仅知道我正在捕获 anstd::logic_error
并不能证明确实捕获了异常的特定实例。
Catch2 为此提供了REQUIRE_THROWS_MATCHES()
宏。
这是我如何将它与Equals
匹配器一起使用的示例:
CATCH_REQUIRE_THROWS_MATCHES(
std::make_shared<advgetopt::getopt>(
options_environment
, sub_argc
, sub_argv)
, advgetopt::getopt_exception_logic
, Catch::Matchers::Equals(
"section \"invalid::name\" includes a section separator (::) in \""
+ options_filename
+ "\". We only support one level."));
除非我的异常中有一个强制转换运算符,否则它不会编译。在这种情况下,这很容易,因为我有自己的例外。但我想知道为什么 Catch2 的作者想到使用强制转换std::string
而不是使用what()
函数。
这是我当前的基类异常定义:
class logic_exception_t
: public std::logic_error
, public exception_base_t
{
public:
explicit logic_exception_t( std::string const & what, int const stack_trace_depth = STACK_TRACE_DEPTH );
explicit logic_exception_t( char const * what, int const stack_trace_depth = STACK_TRACE_DEPTH );
virtual ~logic_exception_t() override {}
virtual char const * what() const throw() override;
operator std::string () const;
};
这是operator std::string () const
功能:
logic_exception_t::operator std::string () const
{
return what();
}
是否有另一种方法可以满足 Catch2 要求并允许将异常转换为 anstd::string
而无需创建强制转换运算符?我只是不喜欢演员阵容,这可能会导致其他问题。
注意:我试图使演员表明确,而 Catch2 也不喜欢它。它只是将异常传递给需要std::string
.