0

After reading the google test / mock documentation, I am still unclear about the best approach to take in a certain case. Lets say I have a class A:

class A
{
public:
    virtual bool start(...);

private:
    virtual bool func_a(...);
    virtual bool func_b(...);
    virtual bool func_c(...);
    virtual bool func_d(...);
};

I have created a mock of class A and tested code that uses class A by calling class A’s start function. The start function has some complex logic that either calls func_a, func_b, func_c, or func_d. What’s the best approach to testing the complex logic in class A’s start function in this case?

273K
  • 29,503
  • 10
  • 41
  • 64
  • You may create a derived class that overrides private functions. Each overridden function should add some information in it's private storage (for example, save argument values and fome mark of that this function was called) and call respective method of the parent class. Then you should call `start()` function of the derived class and analyse the data that was stored by private functions. This will allow you to ensure that the call order of those functions is right and that they are called with right arguments. – Innokentiy Alaytsev Dec 07 '17 at 07:28
  • 1
    @InnokentiyAlaytsev suggestion is OK, however it is also a straight way to [change detector test](https://testing.googleblog.com/2015/01/testing-on-toilet-change-detector-tests.html). If it is difficult to test a `start` function due to complicated logic, then it is a signal that your `start` function should probably be refactored. Consider delegating some `start` responsibilities to other classes, or consider extending the `class A` interface (only if it would actually be used, not just for testing purpose). – pptaszni Dec 07 '17 at 09:44
  • Possible duplicate of [How do you unit test private methods?](https://stackoverflow.com/questions/250692/how-do-you-unit-test-private-methods) – Raedwald Dec 14 '17 at 09:06
  • Innokentiy Alaytsev, thanks for the suggestion. In the end, I went with Ptaq666's suggestion as it sounds like the best solution to me. That is, I moved member functions func_a, func_b, func_c and func_d into a new class, which for the sake of this discussion I will call class B . Then, I mocked class B to test the complex logic in class A's start member function. Thanks Ptaq666. – Ryan Heniser Dec 15 '17 at 10:00

1 Answers1

0

You can define that some test are able to access to private members:

friend class FooTest_BarFuncAMustReturnTrue_Test;

Joan Esteban
  • 1,006
  • 12
  • 23