Reputation: 20610
I am trying to setup Agile and TDD environment for MFC applications that require high performance.
Since MFC View/Document are not testable, I am going to make them as dumb as possible and to test the other classes with Boost Test framework. - Please let me know if you know better way or better test framework for this environment.
In order to make TDD work, I think having dependency injection is crucial for loosely coupled structure. How can I achieve this? Any reference or hint will be appreciated.
Upvotes: 1
Views: 147
Reputation:
Use shared_ptr's to interfaces representing your classes.
I am thinking something like
#ifndef CLOCK_HPP_INCLUDED
#define CLOCK_HPP_INCLUDED
#include <boost/shared_ptr.hpp>
class Clock
{
public:
static boost::shared_ptr<Clock> create();
virtual void init() = 0;
virtual double getSeconds() = 0;
virtual void sleepUntilNext(double howMuch) = 0;
protected: // No polymorphic deconstruction because of shared_ptr
~Clock()
{}
};
#endif
Then you could just take the clock implementation in your constructor, or have another method to set the implementation, like setClock or something.
Example of this would be
#include "clock.hpp"
class MyClass
{
public:
myClass(boost::shared_ptr<Clock> aClock) : myClock(aClock)
{
myClock.init();
}
private:
boost::shared_ptr<Clock> myClock;
}
Then in your unit testing you could do something like this:
boost::shared_ptr<Clock> mock = createMockClock();
MyClass b(mock);
mock.assertThatInitIsCalled();
You can also just ignore resources. The shared_ptr's will delete themselves.
The cost to this would be a little performance loss due to dynamic allocation, virtual function calls, and the overhead of shared_ptr.
The benefits would be increased modularity, lowered compile time, easier use of mocking frameworks like googlemock(they require interfaces anyways), and easier resource management(you will never have a null pointer).
Upvotes: 1