ASSERT_TRUE
和 ASSERT_FALSE
都没有在 LibraryTest
类中编译并出现错误。
error C2664: 'std::basic_string<_Elem,_Traits,_Alloc>::basic_string(const std::basic_string<_Elem,_Traits,_Alloc> &)' : cannot convert parameter 1 from 'void' to 'const std::basic_string<_Elem,_Traits,_Alloc> &'
它在我使用的任何 TEST_F
中都有效。
但是 EXPECT_FALSE
在 LibraryTest
类和 TEST_F
方法中编译得很好。
如何在 TEST_F
使用的方法中使用 ASSERT
?
class LibraryTest : public ::testing::Test
{
public:
string create_library(string libName)
{
string libPath = setup_library_file(libName);
LibraryBrowser::reload_models();
ASSERT_FALSE(library_exists_at_path(libPath));
new_library(libName, libPath);
ASSERT_TRUE(library_exists_at_path(libPath));
EXPECT_FALSE(library_exists_at_path(libPath));
return libPath;
}
};
TEST_F(LibraryTest, libraries_changed)
{
string libName = "1xEVTestLibrary";
string libPath = create_library(libName);
}
最佳答案
如果新的 C++ 标准是您项目的一部分,那么您可以简单地解决它。
#if __cplusplus < 201103L
#error lambda is not supported
#endif
void create_library(const string &libName, string &libPath) {
libPath = ...
[]() -> void { ASSERT_FALSE(...); }();
}
或者甚至重新定义那些宏:
mygtest.hpp
#include <gtest/gtest.hpp>
#if __cplusplus < 201103L
#error lambda is not supported
#endif
// gtest asserts rebind with the `void` error workaround (C++11 and higher is required)
#undef ASSERT_TRUE
#define ASSERT_TRUE(condition) []() -> void { GTEST_TEST_BOOLEAN_((condition), #condition, false, true, GTEST_FATAL_FAILURE_); }()
#undef ASSERT_FALSE
#define ASSERT_FALSE(condition) []() -> void { GTEST_TEST_BOOLEAN_(!(condition), #condition, true, false, GTEST_FATAL_FAILURE_); }()
...
关于c++ - 为什么谷歌测试 ASSERT_FALSE 在方法中不起作用,但 EXPECT_FALSE 可以,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17961900/