Question

I have a mock class like this:

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

I want to test whether beginTransaction() is called before calls to doSomething() and doSomethingElse(), and commit() is called at the end. I did it like this:

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

This works, but it overspecifies the expectation. I don't care about the sequence of doSomething() and doSomethingElse(), only that beginTransaction() is called at the very beginning and commit() is called at the very end. Is there any way to do this?

Was it helpful?

Solution

Would something like this work? I haven't been able to test it out yet unfortunately.

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);

This basically says that beginTransaction() must be called, and it must happen before any other call. And then doSomething() and doSomethingElse() can be called any number of times (including 0). Finally, commit() must be called after the calls to the other functions.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top