c++ - How to gmock functions in the same class when they call each other -


suppose here case:

class base() {    virtual func1()=0;    virtual func2()=0;    virtual func3()=0; } class inheritance:public base {    virtual func1(){ func2(); func3() };    virtual func2(){ /* */ };    virtual func3(){ /* */ }; } 

now want unit test func1(), , mock func2() , func3();

so possible mock func2() , func3() while func1() knows call mock-func2() , mock-func3() , not call real func2() , func3()?

by way use class factory init class, real case may little more complicated

thanks~

it's possible describe, it's not pretty. 1 way of doing have mock derive concrete class instead of interface, , create subclass of mock dispatch method calls appropriate superclass depending on configuration member. example:

// class mockbase inherits baseimpl, in turn inherits // 'base' interface class dispatch : public mockbase { public:     void func1() {         return usemockfunc1 ? mockbase::func1() : baseimpl::func1();     }      bool usemockfunc1; }; 

however, requires yet class, mandates mock inherit concrete implementation rather interface, , make programmers want dig eyes out spoon. rather using kind of abomination, worthwhile review design of class you're testing, looking ways decompose separate objects, each of mocked individually.


Comments

Popular posts from this blog

assembly - 8086 TASM: Illegal Indexing Mode -

Java, LWJGL, OpenGL 1.1, decoding BufferedImage to Bytebuffer and binding to OpenGL across classes -

javascript - addthis share facebook and google+ url -