无论是ASSERT_TRUE和ASSERT_FALSE不在编译LibraryTest与错误类。
错误 C2664:“std::basic_string<_Elem,_Traits,_Alloc>::basic_string(const std::basic_string<_Elem,_Traits,_Alloc> &)”:无法将参数 1 从“void”转换为“const std::basic_string” <_Elem,_Traits,_Alloc> &'
它在TEST_F我使用的任何地方都有效。但是在类和方法中EXPECT_FALSE编译都很好。LibraryTestTEST_F
如何ASSERT在 a使用的方法中使用TEST_F?
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);
}
Run Code Online (Sandbox Code Playgroud)
如果新的 C++ 标准是您项目的一部分,那么您可以简单地解决这个问题。
#if __cplusplus < 201103L
#error lambda is not supported
#endif
void create_library(const string &libName, string &libPath) {
libPath = ...
[]() -> void { ASSERT_FALSE(...); }();
}
Run Code Online (Sandbox Code Playgroud)
甚至重新定义这些宏:
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_); }()
...
Run Code Online (Sandbox Code Playgroud)
使用任何 gtest 断言的函数都需要 return void。在你的情况下,你可以这样改变你的功能:
void create_library(const string &libName, string &libPath) {
libPath = ...
ASSERT_FALSE(...)
}
Run Code Online (Sandbox Code Playgroud)
并像这样使用它:
TEST_F(LibraryTest, libraries_changed) {
string libName = "foo";
string libPath;
create_library(libName, libPath);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5276 次 |
| 最近记录: |