Reputation:
I have trying to motivate my team to enhance our development skills by following TDD. It's a great experience but at some of points I noticed most of them stopped doing that due the problems below:
1/ We are developing "brokers" triggered when something happens and designed to perform a set of actions (send emails, process the data, convert files...).
2/ All brokers are loaded when the application starts thanks to a simple Activator.CreateInstance(brokerType)
.
3/ One of our brokers is supposed to extract records from an excel file and try find associated records in a DB. To do so we just use the code below:
public void Process(string file)
{
using (var sun = new SunSystemExcelDataSource(ExcelDatasourceFactory.Create(file)))
{//...}
}
This code calls a factory returning a class (inheriting from IExcelSource
) able to parse the specified excel file (XLS vs XLSX) and then it is passed as a parameter to the SunSystemExcelDataSource
's constructor.
Question: This method becomes hardly testable. I was wondering if you could give us some tips to improve code test-ability.
Generally speaking how do test classes that rely on factories to build themselves?
Code:
public void Initialize(ILog log, AppSettings settings)
{
_log = log;
_hrdb = DbDatasourceFactory.CreateHrdbDatasource(settings.HrdbConnectionString);
_processor = new K2Processor(settings.SettlePaymentWorkflow);
_sunFolderPath = settings.SunSystemExcelFolderPath;
}
Upvotes: 0
Views: 77
Reputation: 7277
Moq library doesn't support Static methods. But we can use Microsoft Shims and Fakes.
https://msdn.microsoft.com/en-us/library/hh549175.aspx
We can still use Moq, but for that we have to modify the code to something like this,
public void Initialize(ILog log, AppSettings settings)
{
Initialize(log, settings, DbDatasourceFactory.CreateHrdbDatasource(settings.HrdbConnectionString);
}
// Unit-test this method,
internal void Initialize(ILog log, AppSettings settings, HrdbDataSource hrdbDataSource)
{
_log = log;
_hrdb = hrdbDataSource;
_processor = new K2Processor(settings.SettlePaymentWorkflow);
_sunFolderPath = settings.SunSystemExcelFolderPath;
}
Upvotes: 0