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

标签 c++ qt boost googletest googlemock

我正在为我的 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’

如果有帮助,VertexRowtypedef对于 boost::shared_array<Vertex>Vertexstruct使用有效的空构造函数。

这是我为测试编写的错误还是与使用 QList 的不兼容?或 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 指出的那样,ON_CALL 没有 WillOnce()但是有 EXPECT_CALL我在 cooking 书中错过了这个..

最佳答案

最简单的解决方案是:

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

编辑:

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

关于c++ - Google Mock 在尝试指定返回值时给出编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7560215/

相关文章:

c++ - Qt5 - 链接 pHash 库 "undefined reference"

android - Android 中的 native OpenCV UnsatisfiedLinkError libopencv_java.so not found

c++ - 在 C++ 中迭代 map<boost::tuple<int, string>, int>

c++ - 禁用 Visual C++ boost 警告

c++ - 用 t 个随机数生成位 vector

c++ - 未定义对`Histogram::Histogram(QWidget*) 的引用

c++ - 如何将 QImage 转换为 QByteArray?

c++ - 如何从内存中读取,就像使用 iostream 从文件中读取一样?

python - 使用 pybind11 绑定(bind) typedef 类型的正确语法是什么?

c++ - Windows 8 SDK 库位置