C++ 在调用另一个模拟函数时更改模拟函数的返回值

C++ 在调用另一个模拟函数时更改模拟函数的返回值,c++,googletest,googlemock,C++,Googletest,Googlemock,假设我有下面的例子 enum class State { IDLE, BUSY, ERROR1, ERROR2 }; class MockActuator : public ActuatorInterface { public: MOCK_METHOD0(doAction, void()); MOCK_METHOD0(getState, State()); }; 我正在测试的模块假设如果调用了doAction(),ge

假设我有下面的例子

enum class State
{
    IDLE,
    BUSY,
    ERROR1,
    ERROR2
};

class MockActuator : public ActuatorInterface
{
    public:
        MOCK_METHOD0(doAction, void());
        MOCK_METHOD0(getState, State());
};

我正在测试的模块假设如果调用了
doAction()
getState()
应该返回
BUSY

我如何在GMock下对这个假设进行编码?我想将
getState()
保留为模拟函数,因为还有其他返回值需要测试

我的第一次尝试如下:

    EXPECT_CALL(actuator, doAction()).Times(1).WillOnce(InvokeWithoutArgs(
                [&](){
                    ON_CALL(actuator, getState()).WillByDefault(Return(State::BUSY));
                }));
但这会产生以下隐晦的错误:

/usr/src/googletest/googlemock/include/gmock/gmock-actions.h:861:64: error: void value not ignored as it ought to be
   Result Perform(const ArgumentTuple&) { return function_impl_(); }

您可以在中看到如何执行此操作的示例

跟着你的课走

首先是一个模仿原始类的假类:

enum class State
{
    IDLE,
    BUSY,
    ERROR1,
    ERROR2
};

class FakeActuator : public ActuatorInterface
{
    public:
        virtual void doAction () { _state = BUSY; }
        virtual State getState () { return _state; }
    private:
        State _state;
};

之后,模拟类:

class MockActuator : public ActuatorInterface {
 public:
  // Normal mock method definitions using gMock.
  MOCK_METHOD(void, doAction, (), (override));
  MOCK_METHOD(State, getState, (), (override));

  // Delegates the default actions of the methods to a FakeActuator object.
  // This must be called *before* the custom ON_CALL() statements.
  void DelegateToFake() {
    ON_CALL(*this, doAction).WillByDefault([this]() {
      fake_.doAction();
    });
    ON_CALL(*this, getState).WillByDefault([this]() {
      return fake_.getState();
    });
  }

 private:
  FakeActuator fake_;  // Keeps an instance of the fake in the mock.
};
正是这个“伪”对象可以在调用之间保持状态。您还可以根据需要创建任意数量的“假”类

以及测试:

TEST(AbcTest, Xyz) {
  MockFoo foo;

  foo.DelegateToFake();  // Enables the fake for delegation.

  // Put your ON_CALL(foo, ...)s here, if any.

  // No action specified, meaning to use the default action.
  EXPECT_CALL(foo, doAction());
  EXPECT_CALL(foo, getState());

  foo.doAction();  // FakeActuator::doAction() is invoked.

  EXPECT_EQ(BUSY, foo.getState());  // FakeActuator::getState() is invoked.
}