Sweetie
Sweetie

Reputation: 1368

How to assert each item of collection using Nunit

I am trying to assert two items in my collection as I have two entities in my collection. One way that i am trying is just doing a foreach loop and asserting them individually like below. But in this approach, my second item in this collection would have different items.I mean to say that It is passing for first item in collection but not for other as my second item has different values to be compared with..

How can I compare my second item in this way. Or if you have any other idea, please let me know.

  foreach (var item in result.Entities)
            {
                Assert.AreEqual("Contractor1", item.ContractorName, "Result not found");
                Assert.AreEqual(1234, item.PKey, "Result not found");
                Assert.AreEqual("Alex", item.Name, "Result not found");
            }

or what i can do is write if condition within foreach that if it is first item then compare with this otherwise use other values??

So, here is what i did::

  foreach (var item in result.Entities)
            {
                if (result.Entities.First() == item)
                {
                  // Assert First Item
                }
                else{
                     //Assert second list
                 }

            }

but i need a better solution.

Upvotes: 4

Views: 12828

Answers (2)

kat1330
kat1330

Reputation: 5332

I will rather do in following way:

  • create list of expected results expectedEntities
  • verify that expected results are the same number as actual results result.Entities
  • verify that every element in result.Entities is same as expected.

Something like this:

Asert.AreEqual(expectedEntities.Count(), result.Entities.Count());

for (var i = 0: i < expectedEntities.Count(); i++)
{
   Assert.AreEqual(expectedEntities[i].PKey, result.Entities[i].PKey, "Result not found");
   Assert.AreEqual(expectedEntities[i].Name, result.Entities[i].Name, "Result not found");
}

Above example can verify that results are same and in same order.

If you don't care about the order you can for loop each expected element query item and check if it is null:

Asert.AreEqual(expectedEntities.Count(), result.Entities.Count());

for (var i = 0: i < expectedEntities.Count(); i++)
{
   var item = result.Entities.Where(i => i.PKey == expectedEntities[i].PKey && i.Name == expectedEntities[i].Name).SingleOrDefault();
   Assert.IsNotNull(item);
}

Upvotes: 1

Max Venediktov
Max Venediktov

Reputation: 382

Your problem is about testing objects for equality by value.

There are several built in methods in nunit to test if collection of objects are equal.

// Same order, same objects
CollectionAssert.AreEqual(IEnumerable<T> expected, IEnumerable<T> actual)

// Same objects, any order
CollectionAssert.AreEquivalent(IEnumerable<T> expected, IEnumerable<T> actual)

But such comparisons will work, only if your objects T can be compared by value.

You define how objects are compared by overriding Equals and GetHashcode methods. This is somewhat basic thing which you must learn to feel comfortable with .Net. And there are plenty of topics about equality in .Net Check this one How to best implement Equals for custom types?

Upvotes: 3

Related Questions