Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assert.AreEqual failing in unit test

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 (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?

like image 550
m.edmondson Avatar asked Mar 03 '11 12:03

m.edmondson


People also ask

What is assert Areequal?

Tests whether the specified objects are equal and throws an exception if the two objects are not equal. Different numeric types are treated as unequal even if the logical values are equal.

Does assert return true?

The Syntax of the assert Statement. An assert statement consists of the assert keyword, the expression or condition to test, and an optional message. The condition is supposed to always be true. If the assertion condition is true, then nothing happens, and your program continues its normal execution.

How to use assert Equals in c#?

Assert. Equal(expected.Name, actual.Name); The first example fails due to the way comparison works for reference types. By default, the equality operation for those types will only assert whether the two objects being compared are the same, namely your variables are pointing to the same object within the memory heap.

How do you compare two objects in assert?

In order to change the way two objects are compared in an assert we only need change the behavior of one of them – the expect value (might change depending on the unit testing framework).


2 Answers

Use CollectionAssert.AreEqual().

like image 157
John W. S. Marvin Avatar answered Oct 10 '22 03:10

John W. S. Marvin


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.

like image 41
CodesInChaos Avatar answered Oct 10 '22 03:10

CodesInChaos