Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在C中单元测试Textrenderer.DrawText方法#_C#_Unit Testing - Fatal编程技术网

C# 如何在C中单元测试Textrenderer.DrawText方法#

C# 如何在C中单元测试Textrenderer.DrawText方法#,c#,unit-testing,C#,Unit Testing,我有一个用于扩展ComboBox的测试应用程序。 下面代码中给出的所有字符串项都在我的测试仪应用程序的组合框项中。如何对上述方法进行单元测试,因为它返回void? 测试TextRenderer.Drawtext的另一种方法是什么?是否有任何替代方法来测试用于绘制组合框文本的OnDrawItem方法 public static void DrawText(IDeviceContext dc, string text, Font font, Point pt, Color foreColor, Te

我有一个用于扩展ComboBox的测试应用程序。 下面代码中给出的所有字符串项都在我的测试仪应用程序的组合框项中。如何对上述方法进行单元测试,因为它返回void? 测试TextRenderer.Drawtext的另一种方法是什么?是否有任何替代方法来测试用于绘制组合框文本的OnDrawItem方法

public static void DrawText(IDeviceContext dc, string text, Font font, Point pt, Color foreColor, TextFormatFlags flags);
[TestMethod()]
public void ExtendedComboBoxOnDrawPrefixTest()
{
ExtendedComboBox cboTest=新的ExtendedComboBox();
//具有特殊字符的字符串列表。
字符串[]项={
“&One”,
“T&wo”,
“电子公告”,
"Am@persat",
“H#ash”,
“美元符号”,
“Perc%ent”,
“Ci^rcumflex”,
“阿斯特*埃里斯克”,
“海芬”,
“Und_erscore”,
“pl+us”,
“Equ=als”,
“上校:开”,
“分号;冒号”,
“Co'mma”,
“倒逗号”,
“阿波斯战利品”,
“皮普| e”,
“打开{大括号”,
“关闭}大括号”,
“OpenBr[acket”,
“CloseBr]acket”,
“背部\\睫毛”,
“转发SL/ash”,
“莱斯特坦”,
“Questio?nMark”,
“不做”,
“三”,
“四”,
“五个”,
“六”,
“对于组合框来说,这是一个非常长的字符串值。”
};
cboTest.Items.AddRange(Items);
//测试所有项目是否具有相同类型的前缀
对于(int index=0;index
这是一种方法,您不应该验证BCL方法的行为

在UTs中有一个假设:BCL的方法和类工作正常。如果验证BCL的方法,则必须验证
int
byte
ToString()
等的行为(因为您不能信任您的基础结构)

底线是您不应该验证BCL类的行为(
Microsoft
已经为您完成了…)。实际上,您应该假设该方法工作正常(而不是验证),然后验证您使用的方法是否具有正确的参数

为了帮助我的开发人员,我创建了一个流程图,演示了当他们面对这样的问题时如何采取行动:(在我们的研发中,我们发现它非常有用。我们喜欢它影响我们研发的方式…)

在您的情况下,预期行为似乎是可见的,因此您可以通过UI验证它,作为验收/集成/组件测试的一部分

如果您仍然希望将其作为UT进行验证,而您的测试框架不允许您验证(
Rhino Mocks
Moq
等)此方法,则应包装该方法或使用其他工具(如等)测试该方法

下面的代码片段演示了使用
MsFakes
设置方法期望值的方法:

[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;
}

通常情况下,您不会对返回void或不改变某个对象状态的方法进行单元测试。我认为进行单元测试的唯一方法是将输出呈现到某个图像文件,并手动验证它们是否正确,但如果方法调用成功,则没有一种简单的自动方法使其失败(不会抛出
),这是你唯一可以测试的东西。@RonBeyer准确地说,在这种情况下,OP不应该测试这种方法,因为微软已经为我们做了。(这是一种BCL方法)在下面的回答中,我解释了更多。一个漂亮的图表,但我会将你看到的任何地方的“DoIt”替换为“is it”。不确定这是我的强迫症还是什么,但我看到了”这张图表是我的初稿(我在空闲时间在自己的电脑上创建的)。这张图表已经有将近2年的历史了,我不得不在发布之前纠正语法错误(在我以前的工作电脑上)。
    [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....
    }