How to assert that two list contains elements with the same public properties in NUnit?

后端 未结 10 1742
日久生厌
日久生厌 2021-02-04 02:20

I want to assert that the elements of two list contains values that I expected, something like:

var foundCollection = fooManager.LoadFoo();
var expectedCollectio         


        
相关标签:
10条回答
  • 2021-02-04 03:00

    This solved my problem using NUnit's Assertion class from the NUnitCore assembly:

    AssertArrayEqualsByElements(list1.ToArray(), list2.ToArray());
    
    0 讨论(0)
  • 2021-02-04 03:05

    Simple code explaining how to use the IComparer

    using System.Collections;
    using System.Collections.Generic;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    
    namespace CollectionAssert
    {
        [TestClass]
        public class UnitTest1
        {
            [TestMethod]
            public void TestMethod1()
            {
                IComparer collectionComparer = new CollectionComparer();
                var expected = new List<SomeModel>{ new SomeModel { Name = "SomeOne", Age = 40}, new SomeModel{Name="SomeOther", Age = 50}};
                var actual = new List<SomeModel> { new SomeModel { Name = "SomeOne", Age = 40 }, new SomeModel { Name = "SomeOther", Age = 50 } };
                NUnit.Framework.CollectionAssert.AreEqual(expected, actual, collectionComparer);
            }
        }
    
        public class SomeModel
        {
            public string Name { get; set; }
            public int Age { get; set; }
        }
    
        public class CollectionComparer : IComparer, IComparer<SomeModel>
        {
            public int Compare(SomeModel x, SomeModel y)
            {
                if(x == null || y == null) return -1;
                return x.Age == y.Age && x.Name == y.Name ? 0 : -1;
            }
    
            public int Compare(object x, object y)
            {
                var modelX = x as SomeModel;
                var modelY = y as SomeModel;
                return Compare(modelX, modelY);
            }
        }
    }
    
    0 讨论(0)
  • 2021-02-04 03:07

    No, NUnit has no such mechanism as of current state. You'll have to roll your own assertion logic. Either as separate method, or utilizing Has.All.Matches:

    Assert.That(found, Has.All.Matches<Foo>(f => IsInExpected(f, expected)));
    
    private bool IsInExpected(Foo item, IEnumerable<Foo> expected)
    {
        var matchedItem = expected.FirstOrDefault(f => 
            f.Bar1 == item.Bar1 &&
            f.Bar2 == item.Bar2 &&
            f.Bar3 == item.Bar3
        );
    
        return matchedItem != null;
    }
    

    This of course assumes you know all relevant properties upfront (otherwise, IsInExpected will have to resort to reflection) and that element order is not relevant.

    (And your assumption was correct, NUnit's collection asserts use default comparers for types, which in most cases of user defined ones will be object's ReferenceEquals)

    0 讨论(0)
  • 2021-02-04 03:08

    Using Has.All.Matches() works very well for comparing a found collection to the expected collection. However, it is not necessary to define the predicate used by Has.All.Matches() as a separate function. For relatively simple comparisons, the predicate can be included as part of the lambda expression like this.

    Assert.That(found, Has.All.Matches<Foo>(f => 
        expected.Any(e =>
            f.Bar1 == e.Bar1 &&
            f.Bar2 == e.Bar2 &&
            f.Bar3= = e.Bar3)));
    

    Now, while this assertion will ensure that every entry in the found collection also exists in the expected collection, it does not prove the reverse, namely that every entry in the expected collection is contained in the found collection. So, when it is important to know that found and expected contain are semantically equivalent (i.e., they contain the same semantically equivalent entries), we must add an additional assertion.

    The simplest choice is to add the following.

    Assert.AreEqual(found.Count() == expected.Count());
    

    For those who prefer a bigger hammer, the following assertion could be used instead.

    Assert.That(expected, Has.All.Matches<Foo>(e => 
        found.Any(f =>
            e.Bar1 == f.Bar1 &&
            e.Bar2 == f.Bar2 &&
            e.Bar3= = f.Bar3)));
    

    By using the first assertion above in conjunction with either the second (preferred) or third assertion, we have now proven that the two collections are semantically the same.

    0 讨论(0)
提交回复
热议问题