Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does this test method fail?

Here's my test function (c#, visual studio 2010):

[TestMethod()]
public void TestGetRelevantWeeks()
{
List<sbyte> expected = new List<sbyte>() { 2, 1, 52, 51, 50, 49, 48, 47, 46, 45 };
List<sbyte> actual = new List<sbyte>() { 2, 1, 52, 51, 50, 49, 48, 47, 46, 45 };
Assert.AreEqual<List<sbyte>>(expected, actual);
}

Exception: Failed TestGetRelevantWeek Assert.AreEqual failed.
Expected:System.Collections.Generic.List 1[System.SByte].
Actual:System.Collections.Generic.List 1[System.SByte].


Does AreEqual only check equality of the reference, not the contents?

But then, the exception message would be confusing. I also couldn't find a documentation of the default equality comparer for a generic list.

Could you help to clarify why the test fails and what would be solutions for testing the equality of the contents of both lists?

Kind regards

like image 704
nabulke Avatar asked Feb 22 '11 08:02

nabulke


People also ask

What causes test failure?

The Three Common Causes of Exam Failure. There are three main ways that students of all ages can sabotage themselves in exams and bed up with an exam results fail: poor exam technique, poor revision and weak understanding of the subject itself. These can all lead to a bad day in the school exam hall.

What does test fail mean?

Test Failure means a break or rupture that occurs during strength proof testing of transmission or gathering lines that are of such magnitude as to require repair before continuation of the test.

What happens when a test case fails?

Test cases usually fail due to server and network issues, an unresponsive application or validation failure, or even due to scripting issues. When failures occur, it is necessary to handle these test cases and rerun them to get the desired output. An efficient way to do this is to use the TestNG suite.

How do you handle failing tests?

Remind yourself that these feelings are temporary by engaging in activities that bring you joy — doing so reinforces that life goes on and everything will be okay. As little as a few minutes doodling, gardening, reading, dancing, or singing along to your favorite song may ameliorate your mood.


1 Answers

The Assert.AreEqual() method does a reference equality test as you expected.

Assuming you're using .Net 3.5 or above, you can do this:

using System.Linq;

Assert.IsTrue(expected.SequenceEqual(actual));

Edit: Clarified when this option is available.

like image 65
Jackson Pope Avatar answered Nov 15 '22 16:11

Jackson Pope