Aquena
Aquena

Reputation: 13

C# Unit Test Visual Studio 2013 Test Failed

I wrote a class Complex and class for unit tests. When the + operator performs tests crashes my error. Why?

Complex:

    private double re,im;

    public Complex(double re = 0.0, double im = 0.0)
    {
        this.re = re;
        this.im = im;
    }

    public static Complex operator +(Complex C1, Complex C2)
    {

        return new Complex(C1.re+C2.re,C1.im+C2.im);
    }

Unit test:

[TestClass]
public class UnitTest1
{
    [TestMethod]
    public void ComplexPlus()
    {
        Complex c1 = new Complex(4.6, 1.6), c2 = new Complex(6.63, 2.67);
        Complex c3 = c1 + c2;
        Complex c4 =  new Complex(11.23, 4.27);
        Assert.AreEqual(c3,c4 );
    }
}

Upvotes: 0

Views: 127

Answers (2)

user1618077
user1618077

Reputation: 9

It's look your declared the same class in 2 projects. Delete one and add reference from one project to other (add reference to base project to test project).

Upvotes: 0

mybirthname
mybirthname

Reputation: 18127

The problem is that you are call AreEqual to the both classes Complex. You should check if their properties are equal !

Here is the code which you need.

Assert.AreEqual(c3.Re,c4.Re);
Assert.AreEqual(c3.Im,c4.Im);

EDIT: Add properties

private double _re, _im;

public Complex(double re = 0.0, double im = 0.0)
{
    _re = re;
    _im = im;
}

public double Re
{
     get { return _re; }
     set { _re = value; }//or just get !
}

public double Im
{
     get { return _re; }
     set { _re = value; }//or just get !
}

And the check will be this.

Assert.AreEqual(c3.Re,c4.Re);
Assert.AreEqual(c3.Im,c4.Im);

Upvotes: 1

Related Questions