I have the following method, which I want to perform a unit test on. Change can be made for the method signature:
public void PrintNumber()
{
Enumerable.Range(1, 100).ToList().ForEach(x =>
{
if (x % 3 == 0 && x % 5 == 0)
Console.WriteLine("[35]");
else if (x % 3 == 0)
Console.WriteLine("[3]");
else if (x % 5 == 0)
Console.WriteLine("[5]");
else
Console.WriteLine(x.ToString());
});
}
I have my own solution, but I want to find out if my version is the best.
Thanks!
In order to unit test this method, you need to wrap your
Console.WriteLine()with a proxy class, and inject that class into your method.With this structure you change your method signature to
PrintNumber(IWriter writer)and call the writer. In your test method you inject theStubWriter, in production you inject theConsoleWriter.