code3
code3

Reputation: 91

Is there a way to only mock functions only when I need in Google mock?

I have a mock

class mockA: public A{
    public:
        .......
        MOCK_MOTHOD0(functionB, bool());
}

and functionB is a protected virtual function in class A. I can use the mock method in my test like

 TEST(test,testA){
    .....
    mockA objA;
    EXPECT_CALL(objA, functionB()).WillOnce(Return(true));
}

which works well. But i also have some test cases that I do not want to mock functionB. What should I do? I can think of one way is that create another mock class with the exact same content as mockA but not having line MOCK_MOTHOD0(functionB, bool());. Is there a better way? Thanks.

Upvotes: 1

Views: 1274

Answers (1)

Quarra
Quarra

Reputation: 2705

Seems like http://google.github.io/googletest/gmock_cook_book.html#delegating-calls-to-a-parent-class solves exactly what you want. In all tests you just should use EXPECT_CALL, but for the special test - delegate to the concrete class.

Upvotes: 1

Related Questions