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

I have a tester application which for my ExtendedComboBox. All the String items given below in code are there in my ComboBox items in my tester Application. How to unit test above method because it returns void? What is the other way to test TextRenderer.Drawtext? Is there any replacement to test OnDrawItem method for drawing the 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;
}
Old Fox
  • 8,629
  • 4
  • 34
  • 52
Ayush Baunthiyal
  • 141
  • 1
  • 11
  • 1
    Typically you don't unit test methods that return void or don't mutate the state of some object. The only way I could think of unit testing this is to render the output to some image file and manually verify that they look correct, but there isn't an easy automated way to make this fail if the method call succeeds (doesn't `throw`), which is about the only thing you could test. – Ron Beyer Aug 04 '15 at 13:48
  • @RonBeyer accually in this case, the OP shouldn't test the method because Microsoft already did it for us.(It's a BCL method) Below in my answer I explain more about it. – Old Fox Aug 08 '15 at 22:17

1 Answers1

8

It's a BCL method, you shouldn't verify the behavior of a BCL methods.

In UTs there is one assumption; The BCL's methods and classes works correctly. If you verify BCL's methods then you'll have to verify the behavior of int, byte, ToString() and etc...(because you can't trust your infrastructure)

The bottom line you shouldn't verify the behavior of BCL classes(Microsoft already done it for you...). Actually you should assume that the method works correctly(instead of verifying it), then verify that you are using the method with the right parameters.

In order to help my developers, I created a flowchart which demonstrate how to act when they face a such a problem: (In our R&D we found it very usable. we loved the way it affected our R&D...)

enter image description here

In your case, it seems that the expected behavior is viewable, therefore you can verify it through the UI as a part of your Acceptance/Integration/Component tests...

If you still want to verify it as an UT and your test framework doesn't allow you to verify(Rhino Mocks, Moq etc..) this method, you should wrap the method or test the method using an additional tools such MsFakes, Typemock Isolator and etc...

The following snippet demonstrate the way to set an expectation on the method 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....
    }
Old Fox
  • 8,629
  • 4
  • 34
  • 52
  • 1
    A pretty chart, but I would replace anywhere you see "Does it" with "Is it". Not sure if its my OCD or what but seeing "does it easy to..." or "does it worth the effort" is like gravel in my eyes :) +1 – Ron Beyer Aug 09 '15 at 00:57
  • Thanks. It's not your OCD, it's my dyslexia :-) . This chart is the first draft(I created the chart on my own computer in my free time). The chart is almost 2 years old and I had to fix my grammar mistakes(in my previous job computer) before I published it. – Old Fox Aug 09 '15 at 05:18