简体   繁体   English

Visual Studio单元测试拒绝运行

[英]Visual Studio unit test refuses to run

I am starting to test a school project using the Visual Studio and the built in unit tester. 我开始使用Visual Studio和内置的单元测试器测试学校项目。 The project is a class library written in C#. 该项目是用C#编写的类库。 All of my tests up to this point have worked. 到目前为止,我的所有测试均有效。 However, I still have 1 test that will not run. 但是,我仍然有1个测试无法运行。 It's not passing or failing, it simply doesn't run. 它没有通过或失败,它只是无法运行。 There are no error messages given and I can not get it to run or debug or anything. 没有给出错误消息,我无法运行或调试它或任何东西。 Here is the test I am attempting: 这是我正在尝试的测试:

[TestMethod()]
    public void PublicDecimalEqualityTest2()
    {
        Formula form1 = new Formula("2.3232000+3.00");
        Formula form2 = new Formula("2.3232+3.0000");
        Assert.IsTrue(form1==form2);
    }

The "==" operator for my class is defined correctly. 我的课程的“ ==”运算符已正确定义。 Strangely enough this test runs and passes: 奇怪的是,此测试运行并通过:

[TestMethod()]
    public void PublicDecimalEqualityTest()
    {
        Formula form1 = new Formula("2.3232000+3.00");
        Formula form2 = new Formula("2.3232+3.0000");
        Assert.IsTrue(form1.Equals(form2));
    }

Any idea why the first test posted won't run? 知道为什么第一次发布的测试无法运行吗?

Edit: Here is the code for the == operator: 编辑:这是==运算符的代码:

public static bool operator ==(Formula f1, Formula f2) {
    if (f1==null && f2==null)
    { return true; }
    if (f1==null || f2==null)
    {return false;}
    if (f1.GetFormulaBasic()==f2.GetFormulaBasic())
    { return true; }
    else
    { return false;}
}

GetFormulaBasic() simply returns a private string from the class. GetFormulaBasic()只是从类中返回一个私有字符串。 Hope this helps. 希望这可以帮助。

My guess was correct. 我的猜测是正确的。 You are calling the operator == inside your implementation when you are checking for null. 当您检查null时,您正在实现中调用运算符== Replace == with Object.ReferenceEquals to test for null inside the operator. Object.ReferenceEquals替换==以测试运算符内部是否为null。 Here it is, simplified a little: 在这里,简化了一下:

public static bool operator ==(Formula f1, Formula f2)
{
    if (object.ReferenceEquals(f1, f2))
    { 
        return true; 
    }
    if (object.ReferenceEquals(f1, null) || object.ReferenceEquals(f2, null))
    {
        return false;
    }

    return f1.GetFormulaBasic() == f2.GetFormulaBasic();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM