Make unit testing in C# and using MSTest only for the next code: add everything needed…
- User
Make unit testing in C# and using MSTest only for the next code: add everything needed to the original class to make the testing possible without actually printing
public delegate void OnPrintEndEventHandler(); public static void PrintString(string s, string font = "Times New Roman", int fontSize = 12, int xMargin = 0, int yMargin = 0, OnPrintEndEventHandler OnPrintEnd = null) { int linesPrinted = 0; int pagesPrinted = 1; bool multiPage = false; string[] str = s.Split('\n'); PrintDocument p = new PrintDocument(); float y2Margin = p.DefaultPageSettings.PrintableArea.Height - yMargin; float x2Margin = p.DefaultPageSettings.PrintableArea.Width - xMargin; p.PrintPage += delegate (object sender1, PrintPageEventArgs e1) { Font myFont = new Font( font, fontSize, System.Drawing.FontStyle.Bold); float lineHeight = myFont.GetHeight(e1.Graphics) + 4; float yLineTop = yMargin; int numLines = s.Count(c => c.Equals('\n')) + 1; e1.HasMorePages = false; while (linesPrinted < numLines) { if (yLineTop + lineHeight > y2Margin) { e1.HasMorePages = true; multiPage = true; pagesPrinted = PrintPageLine(font, yMargin, x2Margin, e1, pagesPrinted, y2Margin); return; } e1.Graphics.DrawString( str[linesPrinted], myFont, new SolidBrush(Color.Black), new PointF(xMargin, yLineTop)); linesPrinted++; yLineTop += lineHeight; } if (multiPage) { PrintPageLine(font, yMargin, x2Margin, e1, pagesPrinted, y2Margin); } }; if (OnPrintEnd != null) { p.EndPrint += (o, e) => { if (!e.Cancel) { OnPrintEnd?.Invoke(); } }; } try { p.Print(); } catch (Exception ex) { //error } } - Assistant
- User
it doesn't work as the printstring calls the OS printing window but I don't want to make it print, I just want to check if the string printed was correct
- Assistant
- User
can that be done without using MOQ library and only MSTest?
- Assistant
Yes, you can achieve the same result without using Moq, by creating a mock implementation of
PrintDocumentandGraphicsclasses. Here is how you can rewrite theSetupandPrintString_ShouldPrintText_WhenValidInputmethods without Moq:private class MockPrintDocument : PrintDocument { public override void Print() { PrintPageEventArgs e = new PrintPageEventArgs(new GraphicsWrapper()); OnPrintPage(this, e); } public class GraphicsWrapper : Graphics { public override void DrawString(string s, Font font, Brush brush, float x, float y) { ActualStrings.Add(s); } // Implement the rest of the members in the Graphics class } } [TestMethod] public void PrintString_ShouldPrintText_WhenValidInput() { // arrange string input = "Test\nText\nTo\nPrint\n"; string expectedOutput = "Test\nText\nTo\nPrint\n"; // Setup mock Graphics to record all calls to DrawString List<string> actualDrawStrings = new List<string>(); MockPrintDocument.GraphicsWrapper mockGraphics = new MockPrintDocument.GraphicsWrapper(); mockGraphics.ActualStrings = actualDrawStrings; // act PrintString(input, p => new MockPrintDocument()); // assert CollectionAssert.AreEqual(expectedOutput.Split('\n'), actualDrawStrings); }