Arseni Mourzenko
Arseni Mourzenko

Reputation: 52321

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

I'm just starting to use gMock (gTest 1.11.0). The call expectations work correctly, but when I try to indicate the value which needs to be returned during a call to a mock, I encounter a compile-time error that I don't understand.

Here's the relevant piece of code:

#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.
    ...
}

Here's the error:

/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 for this error doesn't give anything relevant. I imagine that there is a typo in my code, but I can't figure out where it is.

Upvotes: 0

Views: 1144

Answers (1)

nhatnq
nhatnq

Reputation: 1193

You may need to specify using ::testing::Return;

Upvotes: 3

Related Questions