1

我正在为我的 C++/Qt 应用程序使用 Google Test 和 Google Mock。直到现在,当我尝试这个时,我一直在这个设置上取得了巨大的成功:

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’

如果有帮助,VertexRowtypedefaboost::shared_array<Vertex>并且Vertexstruct带有有效空构造函数的 a。

这是我为测试编写的内容中的错误,还是与使用QListor不兼容shared_array


解决方案 遵循 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 指出的那样,没有 WillOnce() forON_CALL但有 forEXPECT_CALL我在烹饪书中错过了这个..

4

1 回答 1

2

最简单的解决方案是:

EXPECT_CALL(surface, popAllRows_impl()).WillOnce(Return(rowList));

编辑 :

ON_CALLWillByDefault,但没有WillOnce方法。检查gmock 食谱

于 2011-09-26T19:29:19.417 回答