Manjunath R
Manjunath R

Reputation: 1

Assign mock object to protected member

class foo
{
    // some functions which uses class member t
    protected:   
    Test t;
};

Class Test
{
    // some functions

}

and I mocked the class test and how to assign the mock object to class foo? because I am going to test foo class.

Upvotes: 0

Views: 261

Answers (1)

Tobias L.
Tobias L.

Reputation: 107

Does I undestand you right? Your productive code is in the class foo and it uses functionality which is provided by a class. In your case Test? Please use Dependency Injection to avoid such problems. Create an interface which Test is deriving from it. For example:

// Productive Code
class TestInterface {
    virtual void TestMethod() = 0;
};
class ProductiveTest : public TestInterface {
...
}
class foo
{
    public:
    foo(TestInterface const& t) : t_(t) {}
    // some functions which uses class member t
    protected:   
    TestInterface& t_;
};

// Test Code
class Test : public TestInterface {
    MOCK_METHOD0(TestMethod, void());
}

So you can test foo also in isolation.

Upvotes: 1

Related Questions