使用GoogleTest验证例外消息 [英] Verifying exception messages with GoogleTest

查看:153
本文介绍了使用GoogleTest验证例外消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以验证异常抛出的消息?目前可以执行:

Is it possible to verify the message thrown by an exception? Currently one can do:

ASSERT_THROW(statement, exception_type)

这是所有罚款和好,但没有在哪里可以找到一种方法来测试e.what()是我真正想要的。这是不可能通过谷歌测试?

which is all fine and good but no where can I find a way to test e.what() is really what I am looking for. Is this not possible via google test?

推荐答案

类似下面的工作。只是捕获异常,然后在 what()调用

Something like the following will work. Just catch the exception somehow and then do EXPECT_STREQ on the what() call:

#include "gtest/gtest.h"

#include <exception>

class myexception: public std::exception
{
  virtual const char* what() const throw()
  {
    return "My exception happened";
  }
} myex;


TEST(except, what)
{
  try {
    throw myex;
  } catch (std::exception& ex) {
      EXPECT_STREQ("My exception happened", ex.what());
  }

}

int main(int argc, char **argv) {
  ::testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}

这篇关于使用GoogleTest验证例外消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆