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

发布于 2024-12-06 11:43:10 字数 2047 浏览 1 评论 0原文

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

如果有帮助,VertexRow is a typedef< /code> 对于 boost::shared_arrayVertex 是一个具有有效空构造函数的 struct

这是我为测试编写的内容中的错误,还是与使用 QListshared_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,我在烹饪书中错过了这个。

I'm using Google Test and Google Mock for my C++/Qt application. I've been having great success with this setup until just now when I tried this:

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));
/* ... */

Attempting to compile this results in the following error message from 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’

If it helps, VertexRow is a typedef for a boost::shared_array<Vertex> and Vertex is a struct with a valid empty constructor.

Is this an error in what I wrote for the test or some incompatibility with using QList or shared_array?


SOLUTION
After following VJo's recommendation, my tests compiled and ran but then crashed:

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.

Because there was no default return for popAllRows_impl(). I added a default:

ON_CALL(surface, popAllRows_impl()).WillByDefault(Return(QList<AbstractSurface::VertexRow>()));

To my SetUp() and all is well. As VJo pointed out, there is no WillOnce() for ON_CALL but there is for EXPECT_CALL and I missed this in the cook book..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花辞树 2024-12-13 11:43:10

最简单的解决方案是:

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

编辑:

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

The simplest solution is :

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

EDIT :

ON_CALL has WillByDefault, but no WillOnce method. Check the gmock cookbook

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文