How to Unit test Textrenderer.DrawText method in C#

前端 未结 1 561
旧巷少年郎
旧巷少年郎 2020-12-04 03:02
public static void DrawText(IDeviceContext dc, string text, Font font, Point pt, Color foreColor, TextFormatFlags flags);

I have a tester applicati

相关标签:
1条回答
  • 2020-12-04 03:37

    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...)

    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....
        }
    
    0 讨论(0)
提交回复
热议问题