How to Unit test Method Textrenderer.DrawText in C #

public static void DrawText(IDeviceContext dc, string text, Font font, Point pt, Color foreColor, TextFormatFlags flags); 

I have a tester application that is for my ExtendedComboBox. All String elements below in the code are contained in my ComboBox components in the tester application. Like the unit test method above, because it returns void? What is another way to test TextRenderer.Drawtext? Is there a replacement for testing the OnDrawItem method for drawing ComboBox text.

 [TestMethod()] public void ExtendedComboBoxOnDrawPrefixTest() { ExtendedComboBox cboTest = new ExtendedComboBox (); // List of strings having special characters. string[] items = { "&One", "T&wo", "E!xclamation", " Am@persat ", "H#ash", "Dollar$Sign", "Perc%ent", "Ci^rcumflex", "Ast*erisk", "Hy-phen", "Und_erscore", "pl+us", "Equ=als", "Col:on", "Semi;colon", "Co'mma", "Inverted\"Comma", "Apos'trophe", "Pip|e", "Open{Brace", "Close}Brace", "OpenBr[acket", "CloseBr]acket", "BackS\\lash", "ForwardSl/ash", "LessT<han", "Greate>rThan", "Questio?nMark", "Do.t", "Three", "Four", "Five", "Six", "This is a really extremely long string value for a combobox to display." }; cboTest.Items.AddRange(items); // To test that all the items have the same kind of prefixes for (int index = 0; index < cboTest.Items.Count; index++) { String expectedText = GetExtendedComboBoxText(cboTest, items[index]); Assert.AreEqual(items[index], , String.Format("Item '{0}' returned an string", cboTest.Items[index])); } } /// <summary> /// Compare the ComboBoxText of the passed string with respect to the DC, Font, ForeColor and TextFormatFlags. /// Draw the item /// </summary> private string GetExtendedComboBoxText(Control cboTest, string itemToTest) { TextFormatFlags textFormatflags = TextFormatFlags.NoPrefix; Color foreColor = SystemColors.HighlightText; return (TextRenderer.DrawText(cboTest.CreateGraphics(), itemToTest, cboTest.Font, new Point(cboTest.Bounds.X, cboTest.Bounds.Y), foreColor, textFormatflags)).Text; } 
+2
source share
1 answer

This is a BCL method, you should not test the behavior of BCL methods.

UT has one assumption; BCL methods and classes work correctly. If you test the BCL methods, you will need to check the behavior of int , byte , ToString() , etc ... (because you cannot trust your infrastructure)

On the bottom line, you should not check the behavior of the BCL classes ( Microsoft has already done this for you ...). In fact, you should assume that the method works correctly (instead of checking), and then make sure that you use the method with the correct parameters.

To help my developers, I created a flowchart that demonstrates how to act when faced with such a problem: (In our R&D, we found it very convenient to use. We liked how this affected our R&D .. .)

enter image description here

In your case, it seems that the expected behavior is viewable, so you can test it through the user interface as part of your Acceptance / Integration / Component tests ...

If you still want to test it as UT, and your test environment does not allow you to test ( Rhino Mocks , Moq , etc.) this method, you should wrap the method or test the method using additional tools such as MsFakes , Typemock Isolator etc...

The following snippet demonstrates how to set a method's wait using MsFakes :

  [TestMethod] public void PutExpectationOnDrawText() { var wasExcute = false; System.Windows.Forms.Fakes.ShimTextRenderer .DrawTextIDeviceContextStringFontRectangleColorTextFormatFlags = (context, txt, font, pt, forecolor, flags) => { //the asserts on the orguments... //for example: Assert.IsNotNull(context); Assert.AreNotEqual(String.Empty, txt); //... wasExcute = true; }; //execute the method which is use DrawText Assert.IsTrue(wasExcute);//verify that the method was execute.... } 
+7
source

All Articles