Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/153.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ gtest—确保在某个方法调用之前未调用某个方法,但可以在某个方法调用之后调用该方法_C++_Unit Testing_Googletest_Gmock - Fatal编程技术网

C++ gtest—确保在某个方法调用之前未调用某个方法,但可以在某个方法调用之后调用该方法

C++ gtest—确保在某个方法调用之前未调用某个方法,但可以在某个方法调用之后调用该方法,c++,unit-testing,googletest,gmock,C++,Unit Testing,Googletest,Gmock,如何测试setState()方法在subscribe()之前未被调用,同时允许但不强制在之后调用它 下面的代码显示了我想要实现的目标 这是我要测试的方法: ... void FirstState::enter() { mInfoSubscription.subscribe(mInfoListener); mStateMachine.setState(mFactory.makeState(StateId::SecondState)); } ... 这是一个单元测试: cla

如何测试
setState()
方法在
subscribe()
之前未被调用,同时允许但不强制在之后调用它

下面的代码显示了我想要实现的目标

这是我要测试的方法:

...

void FirstState::enter()
{
    mInfoSubscription.subscribe(mInfoListener);

    mStateMachine.setState(mFactory.makeState(StateId::SecondState));
}

...
这是一个单元测试:

class FirstStateTest : public ::testing::Test {
protected:
    FirstStateTest()
        : mFirstState{mMockStateMachine, mMockStatesFactory,
                      mMockInfoSubscription, mMockInfoListener}
    {
    }

    NiceMock<MockStateMachine> mMockStateMachine;
    NiceMock<MockStatesFactory> mMockStatesFactory;
    NiceMock<MockInfoSubscription> mMockInfoSubscription;
    NiceMock<MockInfoListener> mMockInfoListener;

    FirstState mFirstState;
};

TEST_F(FirstStateTest, ensure_that_subscription_is_done_before_state_change)
{
    // This method must not be called before "subscribe()".
    EXPECT_CALL(mMockStateMachine, doSetState(_)).Times(0);  // doesn't work the way I want

    EXPECT_CALL(mMockInfoSubscription, subscribe(_)).Times(1);

    // At this moment it doesn't matter if this method is called or not.
    EXPECT_CALL(mMockStateMachine, doSetState(_)).Times(AtLeast(0));  // doesn't work the way I want

    mFirstState.enter();
}

// other unit tests ...
...
您可以使用以确保预期的调用是有序的

TEST_F(FirstStateTest, ensure_that_subscription_is_done_before_state_change) {
  InSequence expect_calls_in_order;

  // `subscribe` must be called first.
  EXPECT_CALL(mMockInfoSubscription, subscribe(_)).Times(1);

  // After the `subscribe` call, expect any number of SetState calls.
  EXPECT_CALL(mMockStateMachine, doSetState(_)).Times(AtLeast(0));

  mFirstState.enter();
}

InSequence
是一种声明订单的简单方法,但它适用于其范围内的所有
EXPECT\u CALL
s。如果需要更灵活的指定顺序的方法,请使用
顺序
,请参阅
TEST_F(FirstStateTest, ensure_that_subscription_is_done_before_state_change) {
  InSequence expect_calls_in_order;

  // `subscribe` must be called first.
  EXPECT_CALL(mMockInfoSubscription, subscribe(_)).Times(1);

  // After the `subscribe` call, expect any number of SetState calls.
  EXPECT_CALL(mMockStateMachine, doSetState(_)).Times(AtLeast(0));

  mFirstState.enter();
}