Question

Consider this class under test:

class A
{
public:
    bool isTrue();
    void doSomething();
};

I want to test doSomething which calls isTrue in its implementation.

But I also want to set expectations on the isTrue method. Is there a best practice with google test how one can mock methods of the class under test?

Was it helpful?

Solution

Turning my comment into an answer...

There's a little hack you can use. Define a derived class like so:

class B : public A {
public:
    MOCK_METHOD0(isTrue, bool());

    using A::doSomething;
};

This obviously only works if isTrue is virtual.

Beware: Check your company's testing guidelines regarding "code made especially for testing" instead of testing the real code. In your situation (legacy codebase) this might be okay.

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