Google Mock在尝试指定返回值时给出编译错误
Google Mock giving compile error when attempting to specify a return value
我使用谷歌测试和谷歌模拟我的c++/Qt应用程序。我使用这个设置一直很成功,直到现在我尝试了这个:
QList<AbstractSurface::VertexRow> rowList;
for (unsigned i = 0; i < rows; ++i)
{
AbstractSurface::VertexRow curRow(new AbstractSurface::Vertex[cols]);
for (unsigned j = 0; j < cols; ++j)
{
curRow[j] = AbstractSurface::Vertex();
}
rowList.append(curRow);
}
ON_CALL(surface, numRows_impl()).WillByDefault(Return(rows));
ON_CALL(surface, numColumns_impl()).WillByDefault(Return(cols));
ON_CALL(surface, popAllRows_impl()).WillOnce(Return(rowList));
/* ... */
尝试编译此命令会导致gcc发出以下错误消息:
../../3DWaveSurface/test/TestWaterfallPresenter.cc: In member function ‘virtual void<unnamed>::WaterfallPresenterTest_CallingPaintGLCallsPaintRowForEachRowInSurface_Test::TestBody()’:
../../3DWaveSurface/test/TestWaterfallPresenter.cc:140:41: error: ‘class testing::internal::OnCallSpec<QList<boost::shared_array<AbstractSurface::Vertex> >()>’ has no member named ‘WillOnce’
如果有帮助,VertexRow
是typedef
的boost::shared_array<Vertex>
, Vertex
是struct
的有效空构造函数。
这是我为测试写的错误还是与使用QList
或shared_array
不兼容?
<
解决方案/strong>在遵循VJo的建议后,我的测试编译并运行,但随后崩溃:
Stack trace:
: Failure
Uninteresting mock function call - returning default value.
Function call: popAllRows_impl()
The mock function has no default action set, and its return type has no default value set.
The process "/home/corey/development/3DWaveSurface-build-desktop-debug/test/test" crashed.
因为popAllRows_impl()
没有默认返回值。我添加了一个默认值:
ON_CALL(surface, popAllRows_impl()).WillByDefault(Return(QList<AbstractSurface::VertexRow>()));
祝我的SetUp()
万事如意。正如VJo指出的那样,ON_CALL
没有WillOnce(),但EXPECT_CALL
有,我在烹饪书中错过了这个。
最简单的解决方案是:
EXPECT_CALL(surface, popAllRows_impl()).WillOnce(Return(rowList));
编辑:
ON_CALL
有WillByDefault
,但没有WillOnce
方法。查看gmock烹饪书
相关文章:
- std::is_base_of表示ctor编译错误
- Qt5:使用QCommandLineParser类时出现奇怪的编译错误
- 如何修复sfml c++代码编译错误
- 使用 MATLAB 编码器生成C++代码:编译错误"undefined reference to `rgb2gray_tbb_real64'"
- 使用外部SDK工具链文件在VisualStudio上生成项目编译错误
- vscode下的Arduino代码出现意外编译错误
- 第三方 API 中的编译错误 - Visual Studio
- std::cout输出int时出现编译错误
- 奇怪的代码抛出编译错误模板< J,int aSize=10> C2143:语法错误:在"<"之前缺少";"
- 提升图广度优先搜索前置编译错误
- C++ 中的编译错误:未定义对"主"的引用 collect2:错误:ld 返回 1 个退出状态
- 使用带有 ref 参数的成员函数创建线程时出现编译错误
- 我在C++中遇到了这个奇怪的编译错误
- 在C++中使用 Catch 测试框架编译错误"error: expected ';' at end of declaration list"
- 使用 std::enable_if 限制派生类的模板参数时出现编译错误
- 现代OpenGL和GLEW Libraray的编译错误
- C++ 编译错误:意外的类型名称"字符串":预期的表达式
- C ++程序编译错误,找不到/访问文件
- 使用直接大括号初始化时,C++ 编译错误"声明末尾的预期";"
- 为什么传递非静态成员函数会导致编译错误?