I have a method that prints private variable of the class.
public class Test
{
private int number;
public Test(int number)
{
this.number = number;
}
public void PrintValue()
{
Console.WriteLine("Number: " + number);
}
}
How can I create an unit test to make sure it prints value I expected on console?
Make console output a dependency. Since
Console.WriteLinewill redirect its output toConsole.Outproperty, all you need isTextWriterinstance:In real application, you pass it
Console.Outto print to system console. In test, you simply use fake writer (for exampleStringWriterbased onStringBuilder):