baron
baron

Reputation: 11181

Using Assert to compare two objects

Writing test cases for my project, one test I need is to test deletion. This may not exactly be the right way to go about it, but I've stumbled upon something which isn't making sense to me.

Code is like this:

[Test]
private void DeleteFruit()
{
    BuildTestData();
    var f1 = new Fruit("Banana",1,1.5);
    var f2 = new Fruit("Apple",1,1.5);
    fm.DeleteFruit(f1,listOfFruit);
    Assert.That(listOfFruit[1] == f2);
}

Now the fruit object I create line 5 is the object that I know should be in that position (with this specific dataset) after f1 is deleted.

Also if I sit and debug, and manually compare objects listOfFruit[1] and f2 they are the same. But that Assert line fails. What gives?

edit:

Getting the following:

-Assert.Equals should not be used for Assertions

-Assert.AreEqual(fruit1, fruit2); Failed: Expected <FruitProject.Fruit> But was: <FruitProject.Fruit>

-Both fruit1.Equals(fruit2) and fruit1==fruit2 fail ??

Upvotes: 1

Views: 3894

Answers (1)

SysAdmin
SysAdmin

Reputation: 5575

== compares references, since the reference of listOfFruit[1] and f2 are not the same, it fails

C# difference between == and Equals()

Upvotes: 1

Related Questions