© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•17mo ago•
2 replies
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
}
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

❔ How to join 3 collections?
C#CC# / help
3y ago
MSTest: Assembly Initialization Question
C#CC# / help
2w ago
❔ Real-Time MSTest Reporter
C#CC# / help
3y ago
Collections
C#CC# / help
15mo ago