关闭InSequence

时间:2013-10-29 14:14:38

标签: c++ googlemock

我有一个这样的模拟类:

struct MockClass {
    MOCK_METHOD0(beginTransaction, void ());
    MOCK_METHOD0(doSomething, void ());
    MOCK_METHOD0(doSomethingElse, void ());
    MOCK_METHOD0(commit, void ());
};

我想测试是否在调用beginTransaction()doSomething()之前调用doSomethingElse(),并在结束时调用commit()。我是这样做的:

{
    InSequence s;
    EXPECT_CALL(*mockObject, beginTransaction());
    EXPECT_CALL(*mockObject, doSomething());
    EXPECT_CALL(*mockObject, doSomethingElse());
    EXPECT_CALL(*mockObject, commit());
}

这有效,但它超出了预期。我不关心doSomething()doSomethingElse()的序列,只是在开头调用beginTransaction()而在最后调用commit()。有没有办法做到这一点?

1 个答案:

答案 0 :(得分:3)

这样的事情会起作用吗?不幸的是,我还没能测试它。

Expectation e1 = EXPECT_CALL(*mockObject, beginTransaction());
Expectation e2 = EXPECT_CALL(*mockObject, doSomething()).Times(AnyNumber()).After(e1);
Expectation e3 = EXPECT_CALL(*mockObject, doSomethingElse()).Times(AnyNumber()).After(e1);
EXPECT_CALL(*mockObject, commit()).After(e1, e2, e3);

这基本上说必须调用beginTransaction(),并且它必须在任何其他调用之前发生。然后doSomething()doSomethingElse()可以被调用任意次(包括0)。最后,在调用其他函数之后必须调用commit()