unit-testing Method Injection


Example

Method injection is a fine grained way of injecting dependencies into processing. Consider a method that does some processing based on the current date. The current date is hard to change from a test, so it is much easier to pass a date into the method that you want to test.

public void ProcessRecords(DateTime currentDate)
{
    foreach(var record in _records) 
    {
        if (currentDate.Date > record.ProcessDate)
        {
            // Do some processing
        }
    }
}