I am trying to write a unit test for a class that outputs to the console. To capture the console output from this class I decided to create a mock TextWriter object and redirect the console output to it within the test class.
[TestMethod]
public void TestMethod()
{
var outLines = new List<string>();
var mockWriter = new Mock<TextWriter>();
mockWriter.Setup(writer => writer.WriteLine(It.IsAny<string>()))
.Callback<string>(s => outLines.Add(s));
Console.SetOut(mockWriter.Object);
// ConsoleLogger is the class being tested
// It will essentially just print its parameter to the console.
var logger = new ConsoleLogger();
logger.Error("foo");
// Uncommenting this will cause the test to pass
// Console.WriteLine("foo");
Assert.AreEqual(1, outLines.Count);
Assert.AreEqual("foo", outLines[0]);
}
This code does not work. However if I do a Console.WriteLine("foo") within the test it will work.
Even more strangely when debugging the test the console from the Logger class is still being redirected seemingly to nowhere as the unit test does not output anything asides from the Assert exceptions.
So basically my question is why does the mock code not get run when writing from the logger class?
Aucun commentaire:
Enregistrer un commentaire