Google Mock在尝试指定返回值时给出编译错误

Google Mock giving compile error when attempting to specify a return value

本文关键字:编译 错误 返回值 Mock Google      更新时间:2023-10-16

我使用谷歌测试和谷歌模拟我的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’

如果有帮助,VertexRowtypedefboost::shared_array<Vertex>, Vertexstruct的有效空构造函数。

这是我为测试写的错误还是与使用QListshared_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_CALLWillByDefault,但没有WillOnce方法。查看gmock烹饪书