C
C#3mo ago
Hillgrove

MSTest: How to assert 2 collections

I am trying to make a unit test to see if the objects in 1 collection (and the order for that matter) are the same (as in different objects, same values) as the ones in another collection. This fails, as apparently CollectionAssert.AreEqual is a reference test.
[TestClass()]
public class TrophiesRepositoryTests
{
private TrophiesRepository trophyRepo = new();

[TestInitialize]
public void Setup()
{
trophyRepo.Add(new Trophy { Id = 1, Competition = "World Cup", Year = 1998 });
trophyRepo.Add(new Trophy { Id = 2, Competition = "Champions League", Year = 2020 });
trophyRepo.Add(new Trophy { Id = 3, Competition = "Copa America", Year = 2016 });
trophyRepo.Add(new Trophy { Id = 4, Competition = "Euro Cup", Year = 2004 });
trophyRepo.Add(new Trophy { Id = 5, Competition = "Olympics", Year = 2008 });
}

// MethodName_StateUnderTest_ExpectedBehavior

#region Get Tests
[TestMethod()]
public void Get_WithNoParameters_ReturnsEntireList()
{
// Arrange
List<Trophy> expected = new List<Trophy>
{
new Trophy { Id = 1, Competition = "World Cup", Year = 1998 },
new Trophy { Id = 2, Competition = "Champions League", Year = 2020 },
new Trophy { Id = 3, Competition = "Copa America", Year = 2016 },
new Trophy { Id = 4, Competition = "Euro Cup", Year = 2004 },
new Trophy { Id = 5, Competition = "Olympics", Year = 2008 }
};

// Act
List<Trophy> actual = trophyRepo.Get();

// Assert
CollectionAssert.AreEqual(expected, actual);
Assert.AreEqual(5, actual.Count);
}
#endregion
}
[TestClass()]
public class TrophiesRepositoryTests
{
private TrophiesRepository trophyRepo = new();

[TestInitialize]
public void Setup()
{
trophyRepo.Add(new Trophy { Id = 1, Competition = "World Cup", Year = 1998 });
trophyRepo.Add(new Trophy { Id = 2, Competition = "Champions League", Year = 2020 });
trophyRepo.Add(new Trophy { Id = 3, Competition = "Copa America", Year = 2016 });
trophyRepo.Add(new Trophy { Id = 4, Competition = "Euro Cup", Year = 2004 });
trophyRepo.Add(new Trophy { Id = 5, Competition = "Olympics", Year = 2008 });
}

// MethodName_StateUnderTest_ExpectedBehavior

#region Get Tests
[TestMethod()]
public void Get_WithNoParameters_ReturnsEntireList()
{
// Arrange
List<Trophy> expected = new List<Trophy>
{
new Trophy { Id = 1, Competition = "World Cup", Year = 1998 },
new Trophy { Id = 2, Competition = "Champions League", Year = 2020 },
new Trophy { Id = 3, Competition = "Copa America", Year = 2016 },
new Trophy { Id = 4, Competition = "Euro Cup", Year = 2004 },
new Trophy { Id = 5, Competition = "Olympics", Year = 2008 }
};

// Act
List<Trophy> actual = trophyRepo.Get();

// Assert
CollectionAssert.AreEqual(expected, actual);
Assert.AreEqual(5, actual.Count);
}
#endregion
}
2 Replies
cap5lut
cap5lut3mo ago
Tests whether the specified collections are equal and throws an exception if the two collections are not equal. Equality is defined as having the same elements in the same order and quantity. Whether two elements are the same is checked using Equals(Object, Object) method. Different references to the same value are considered equal.
sounds like ur Trophy type doesnt do the equality check correctly. so either fix GetHashCode(), Equals(object) (and probably also implement IEquatable<Trophy>), or use CollectionAssert.AreEqual() overload that takes an IComparer and pass a one in that does the equality check as u want it to be
Hillgrove
HillgroveOP3mo ago
much obliged
Want results from more Discord servers?
Add your server