Comparing Two objects using Assert.AreEqual()

后端 未结 5 1063
眼角桃花
眼角桃花 2020-12-30 20:24

I \'m writing test cases for the first time in visual studio c# i have a method that returns a list of objects and i want to compare it with another list of objects by using

5条回答
  •  滥情空心
    2020-12-30 20:48

    Assert.AreEqual in xUnit on .NET will check if the objects are identical however object identity is different from value equallity it would seem you are looking for value equality. Ie. "Are the objects in my list of the same value?" which is why it "fails" the two lists are not identical even though the values of each object in each list might represent the same value.

    Usually in a testing effort it should be enough to test the count of a collection and key elements.

    var count = listA.Count;
    Assert.AreEqual(count,listB.Count);
    Assert.AreEqual(listA.First(),listB.first());
    Assert.AreEqual(listA.Last(),listB.Last());
    Assert.AreEqual(listA[count/2],listB[count/2]);
    

    The last test doesn't have to be at the middle element and is simply meant to test an element in the list the only reason why it's not a random element is because you would want to be able to reproduce your test results.

提交回复
热议问题