AJ_83
AJ_83

Reputation: 299

CollectionAssert of an nested List

How to assert a nested list too, without 'unnesting' it?

        expected.Add(new Customer{
                Edition = "Cust",
                Rarity = "R",
                ID = 1001,
                Name = "John Doe",
                Types = new List<Type_>{
                    new Type_{
                        ID = 1,
                        Name = "abc"
                    }
                },

Here the assert:

CollectionAssert.AreEqual(expected, actual);

That certainly exclude the nested list.

Upvotes: 0

Views: 1127

Answers (1)

oleksii
oleksii

Reputation: 35895

Something that worked for me:

  • Generate object hash (such as MD5) that includes the content of the list, this is quite a rigid approach and is seldom useful
  • Just write a second assert in the test specifically for that inner list. This is more conventional approach (my favourite)
  • Write a new test to only assert inner list. This is more TDD-purist approach

Edit, here is an example of conventional approach:

var expected = new List<Type_>{
                    new Type_{
                        ID = 1,
                        Name = "abc"
                    };
CollectionAssert.AreEqual(expected, actual.Types);

Upvotes: 2

Related Questions