I have the following unit test:
string MtrlCode = "0";
Assessment target = new Assessment(MtrlCode);
List<string> EdgeCaseSymbolCodes = new List<string>(); //More than 3
EdgeCaseSymbolCodes.Add("FLA");
EdgeCaseSymbolCodes.Add("HAR");
EdgeCaseSymbolCodes.Add("COR");
EdgeCaseSymbolCodes.Add("ENVON");
EdgeCaseSymbolCodes.Add("ENVR");
EdgeCaseSymbolCodes.Add("EXP");
target.HazardSymbols = EdgeCaseSymbolCodes;
List<string> EdgeCaseSymbolCodesExpected = new List<string>(); //Should be 3
EdgeCaseSymbolCodesExpected.Add("FLA");
EdgeCaseSymbolCodesExpected.Add("HAR");
EdgeCaseSymbolCodesExpected.Add("COR");
System.Windows.Forms.MessageBox.Show(EdgeCaseSymbolCodesExpected[0] + EdgeCaseSymbolCodesExpected[1] + EdgeCaseSymbolCodesExpected[2] + " = \n" + target.HazardSymbols[0] + target.HazardSymbols[1] + target.HazardSymbols[2]);
Assert.AreEqual<List<string>>(EdgeCaseSymbolCodesExpected, target.HazardSymbols, "COSHH_2008.Custom_Classes.Assessment.setHazardSymbols Edge Case did not return the expected value.");
Which is testing an edge case 开发者_如何学Python(a time when the List<string>
has more than 3 elements) with the desired output being the return of only the first 3.
Currently the test is failing and I've had to resort to using the MessageBox
to see inside of the test (due to the breakpoints not being hit!).
From what I can see the elements are the same, however I realise there might be something else different with the List<string>
object, but I can't see this as the breakpoints are never been hit.
I can't find the modules window in Visual Studio 2005.
What would your next steps be?
Use CollectionAssert.AreEqual()
.
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.
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]);
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?
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.
The Assert
fails because it verifies whether the two lists are in fact the same instance. They are not; it's two different list instances. You will need to write code to verify that the two lists contains the same set of items.
If you want to compare to List you can create a void function with that code Snippet.
Assert.AreEqual(response.Count, acceptedResponse.Count,"Diferent list length");
for (int i = 0; i < acceptedResponse.Count; i++)
{
Assert.AreEqual(response[i],acceptedResponse[i]);
}
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.
I am also faced(ing) the same problem. As a work around you can check the count of the list, and do a for each and check for contains.
Your actual List seems to contain more items than the expected List. Thus they are not equal.
精彩评论