Assert.AreEqual failing in unit test

后端 未结 10 1347
挽巷
挽巷 2021-01-18 14:54

I have the following unit test:

string MtrlCode = \"0\";
Assessment target = new Assessment(MtrlCode);

List EdgeCaseSymbolCodes = new List<         


        
相关标签:
10条回答
  • 2021-01-18 15:16

    If Assert.Equals uses the default comparer(no idea if it does) then this test will fail since List<T> uses referential equality by default.

    If both lists have the same ordering you can use the linq extensionmethod Enumerable.SequenceEqual to test for element wise equality.

    If you want to consider lists with the same elements equal even with different ordering you could use a.Intersect(b).Count()==a.Unit(b).Count() since there is no SetEqual extension method in Linq.


    And even if it compared by value why do you expect a list containing 3 elements to be equal to a list containing 6 elements?


    As a sidenote: Your naming conventions differ from the .net conventions. Usually local variable names start with lower case letters.

    And I find the line target.HazardSymbols = EdgeCaseSymbolCodes; very strange. Does that mean you have a property of type List<T> with a public setter? I'd rather avoid those since that can lead to different objects using the same instance of a List which can have strange effects if they modify the list they own.

    0 讨论(0)
  • 2021-01-18 15:16

    I don't think Assert.AreEqual supports collections.

    You will have to write your own method that checks the number of elements in the collections and compares them individually.

    You will have to supply your own logic for how you define the equality of collections, ie. if both collections contain the same elements but in different order, are they still equal?

    0 讨论(0)
  • 2021-01-18 15:18

    References to lists are different, try instead

    Assert.AreEqual(EdgeCaseSymbolCodesExpected[0], target.HazardSymbols[0]);
    Assert.AreEqual(EdgeCaseSymbolCodesExpected[1], target.HazardSymbols[1]);
    Assert.AreEqual(EdgeCaseSymbolCodesExpected[2], target.HazardSymbols[2]);
    
    0 讨论(0)
  • 2021-01-18 15:24

    First of all, your lists aren't the same. One has 3 elements and the other has 6, so I wouldn't expect them to be equal in the first place.

    Second, the List class only compares as equals if they're the exact same list, not just lists with the same elements. In your case you want to use something like Assert(EdgeCaseSymbolCodesExpected.SequenceEqual(target.HazardSymbols) which will walk through each element of the lists, comparing each for equality.

    0 讨论(0)
  • 2021-01-18 15:29

    This looks like a case of reference versus value comparison. If you have two different instances of objects with the same property values, by default they will never be 'equal' using default comparisons. You have to compare the values of the objects. Try writing code to compare the values of each instance.

    0 讨论(0)
  • 2021-01-18 15:29

    Your actual List seems to contain more items than the expected List. Thus they are not equal.

    0 讨论(0)
提交回复
热议问题