PragmaTwice
PragmaTwice

Reputation: 13

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. The project is a class library written in C#. All of my tests up to this point have worked. However, I still have 1 test that will not run. 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. Hope this helps.

Upvotes: 1

Views: 140

Answers (1)

Mike Zboray
Mike Zboray

Reputation: 40838

My guess was correct. You are calling the operator == inside your implementation when you are checking for null. Replace == with Object.ReferenceEquals to test for null inside the operator. 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();
}

Upvotes: 5

Related Questions