public void PrintToOutputStream() { var OutputMock = new Mock <TextWriter>(); string outbuffer = ""; OutputMock.Setup(console => console.Write(It.IsAny <string>())) .Callback((string s) => outbuffer += s); OutputMock.Setup(console => console.WriteLine(It.IsAny <string>())) .Callback((string s) => outbuffer += s + Environment.NewLine); OutputMock.Setup(console => console.WriteLine()) .Callback(() => outbuffer += Environment.NewLine); var printer = new CanvasPrinter(OutputMock.Object); ICanvas canvas = new SimpleCanvas(2, 2); printer.Print(canvas); string expected = "----" + Environment.NewLine + "| |" + Environment.NewLine + "| |" + Environment.NewLine + "----" + Environment.NewLine + Environment.NewLine; Assert.Equal(expected, outbuffer); }
public void OverallCanvasTest() { // This is for fun, It is possible to execute all the code with System.Console // when using Moq!! var textReaderMock = new Mock <TextReader>(); var textOutputMock = new Mock <TextWriter>(); var textErrorMock = new Mock <TextWriter>(); string outbuffer = ""; ICanvas canvas = null; textReaderMock.SetupSequence(console => console.ReadLine()) .Returns("C 20 4") .Returns("L 1 2 6 2") .Returns("L 6 3 6 4") .Returns("R 14 1 18 3") .Returns("B 10 3 o") .Returns("Q"); textOutputMock.Setup(console => console.Write(It.IsAny <string>())) .Callback((string s) => outbuffer += s); textOutputMock.Setup(console => console.WriteLine(It.IsAny <string>())) .Callback((string s) => outbuffer += s + Environment.NewLine); textOutputMock.Setup(console => console.WriteLine()) .Callback(() => outbuffer += Environment.NewLine); var canvasPrinterMock = new Mock <ICanvasPrinter>(); canvasPrinterMock.Setup(printer => printer.Print(It.IsAny <ICanvas>())) .Callback((ICanvas c) => canvas = c); var service = new DrawingService( textReaderMock.Object, textOutputMock.Object, textErrorMock.Object, canvasPrinterMock.Object); service.Start(); canvasPrinterMock.Verify(printer => printer.Print(It.IsAny <ICanvas>()), Times.Exactly(5)); // verify the final canvas state string verifyBuffer = ""; var verifyOutputMock = new Mock <TextWriter>(); verifyOutputMock.Setup(console => console.Write(It.IsAny <string>())) .Callback((string s) => verifyBuffer += s); verifyOutputMock.Setup(console => console.WriteLine()) .Callback(() => verifyBuffer += Environment.NewLine); var verifyPrinter = new CanvasPrinter(verifyOutputMock.Object); verifyPrinter.Print(canvas); string expected = "----------------------" + Environment.NewLine + "|oooooooooooooxxxxxoo|" + Environment.NewLine + "|xxxxxxooooooox xoo|" + Environment.NewLine + "| xoooooooxxxxxoo|" + Environment.NewLine + "| xoooooooooooooo|" + Environment.NewLine + "----------------------" + Environment.NewLine + Environment.NewLine; Assert.Equal(expected, verifyBuffer); }