为什么在尝试从模拟中获取 return 值时会出现编译时错误?

Why do I get a compile-time error when trying to return values from a mock?

我刚开始使用 gMock (gTest 1.11.0)。调用预期工作正常,但是当我尝试指示在调用模拟期间需要返回的值时,我遇到了一个我不理解的编译时错误。

这是相关的代码片段:

#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "../src/wrappers/WireWrapper.h"

class MockWire: public WireWrapper {
 public:
    MOCK_METHOD(void, begin, (), (override));
    MOCK_METHOD(void, beginTransmission, (uint8_t address), (override));
    MOCK_METHOD(uint8_t, endTransmission, (), (override));
    ...
}

TEST(Demo, Init) {
    MockWire wire;
    EXPECT_CALL(wire, begin()).Times(1);
    EXPECT_CALL(wire, beginTransmission(0x38)).Times(1);
    EXPECT_CALL(wire, endTransmission()).WillOnce(Return(0)); // ← The error happens here.
    ...
}

这是错误:

/tmp/DemoTests.cpp:26:60: error: ‘Return’ was not declared in this scope
     EXPECT_CALL(wire, endTransmission()).Times(1).WillOnce(Return(0));
                                                            ^~~~~~
/tmp/DemoTests.cpp:26:60: note: suggested alternative:
In file included from /usr/local/include/gmock/gmock.h:59:0,
                 from /tmp/DemoTests.cpp:4:
/usr/local/include/gmock/gmock-actions.h:1254:54: note:   ‘testing::Return’
 inline PolymorphicAction<internal::ReturnVoidAction> Return() {
                                                      ^~~~~~

A Google search 这个错误没有给出任何相关信息。我想我的代码中有错字,但我不知道错在哪里。

您可能需要指定 using ::testing::Return;