I have a class, which uses a timer, to do some time-dependent things. Now, I do create the timer inside the class:
class MyClass
{
public:
Myclass(arg1, arg2, ...)
:m_timer(new Timer(delay))
...
private:
Timer* m_timer;
...
}
The timer has a few functions, like isRunning(
), isExpired()
, reset()
However, when unitesting this class with google test framework, I cannot use the timer in my unit test, since it is a private member.
Another way of doing this, would be to create the timer in the factory for MyClass, and inject it. This way, I would be able mock the timer in my unittest and be able to use the three methods above in my unit test, to do a more extensive test for MyClass.
My question is, which would be the best practice? If I am doing the second alternative(injection), then it feels like I am adapting my class to the test, which is not right? On the other hand, I do get a class which is more testable. Is it possible to say which alternative is better in this case, an maybe in general?