I'm trying to learn implementing TDD with mocking/fake objects. One of the questions I have is how to initialize a dependency in an application which implements TDD? An example from this article Beginning Mocking With Moq 3 shows:
public class OrderWriter
{
private readonly IFileWriter fileWriter;
public OrderWriter(IFileWriter fileWriter)
{
this.fileWriter = fileWriter;
}
public void WriteOrder(Order order)
{
fileWriter.WriteLine(String.Format("{0},{1}", order.OrderId, order.OrderTotal));
}
}
In this example, the constructor takes an IFileWriter
parameter, I suppose because you want to supply the real file writer in case of the actual application, and the fake one for unit test. My question is, in the real application, who will supply this parameter? I suppose it will be the caller of this application. What if it has dependency as well in the constructor? Will the caller code be responsible for that too?
Maybe the better way is to use factory. How would this factory work? And how will the factory be distributed? Will it be in the constructor parameter like in the above manner?