How to mock inheritance chain in C ++ using Google mock

I use Google mock and want to mock the inheritance chain as follows:

class A
{
    // virtual method A1();
}
class B : public A
{
    // virtual method B1();
}

class C : public B
{
   // virtual method C1();
}

When layout A, I need to make fun of A1 (); when layout B, I have to mock A1 () and B1 (); when layout is C, I have to mock C1 (), B1 () and A1 ().

//Mock Definition
class MockA : public A
{
   // mockA1()
}

class MockB : public B
{
   // mockA1()
   // mockB1()
}

class MockC : public C
{
   // mockA1()
   // mockB1() 
   // mockC1()  
}

However, for class C, I have only a new extended function C1 () left. I did not want to scoff at all my parent method, especially when too many methods were defined. But I cannot create an instance of the mockC class if I did not make fun of all the methods inherited from its parent.

, mockC C, mockB. C, mockB pure B., C- > B- > A , .

// I cannot change this to virtual inheritance
class C : public virtual B
{
    //method C1();
}

class mockB : public virtual B
{
    // mthod B1()
}

class mockC: public C, public mockB
{
   // mock method C1();
}

?

+5
1

mock . , - . mockC:

class MockC : public C {
   // mockA1()
   // mockB1() 
   // mockC1()  
}

, , A B C. .

+1

All Articles