Skip to content

How to ignore some mock method calls? #126

Open
@gambr

Description

@gambr

Supposing I have two tests that use the same mock object. In both tests there is a call that causes the call of both methods of the mock. The point is that I want to verify just one mock method for each test. Is there a way to ignore the other mock method calls?
I suppose one option is to add in each test a MOCK_EXPECT of the other method too but what if the methods of the mock are 10? It is not a good idea to add nine MOCK_EXPECT for all the other methods not strictly involved in the current test. Another option is to create a different mock one for each test, but also this way is a little annoying. Is there a clean way to solve this problem?

MOCK_BASE_CLASS(MyObjectMock, MyObject)
{
    MOCK_METHOD(a, 0);
    MOCK_METHOD(b, 0);
};

BOOST_AUTO_TEST_CASE(First) {
    MyObjectMock mock;
    ClassToTest toTest(mock);

    MOCK_EXPECT(processMock.a()).once().returns(2);
    toTest.update();

    BOOST_CHECK_EQUAL(toTest.getA(), 2);
}

BOOST_AUTO_TEST_CASE(Second) {
    MyObjectMock mock;
    ClassToTest toTest(mock);
    
    MOCK_EXPECT(processMock.b()).once().returns(3);
    toTest.update();

    BOOST_CHECK_EQUAL(toTest.getB(), 3);
}

I the previous example toTest.update() calls both a() and b() of the mock.

Thanks

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions