Skip to content

Collections

Derek Greer edited this page Sep 4, 2017 · 2 revisions

By default, ExpectedObjects ignores collection order when comparing collections:

using System.Collections;
using System.Collections.Generic;
using ExpectedObjects;
using Xunit;

namespace ExpectedObjectExamples.Specs
{
    public class EnumerableSpecs
    {
        [Fact]
        public void ComparingEqualCollections_ShouldBeEqual()
        {
            var expected = new TypeWithIEnumerable
              {
                Objects = new List<string> {"test2", "test1"}
              }.ToExpectedObject();

            var actual = new TypeWithIEnumerable 
              {
                Objects = new List<string> {"test1", "test2"}
              };

            expected.ShouldEqual(actual);
        }
    }
}

When the order is important, ExpectedObjects can be configured to use an ordinal comparison:

using System.Collections;
using System.Collections.Generic;
using ExpectedObjects;
using Xunit;

namespace ExpectedObjectExamples.Specs
{
    public class EnumerableSpecs
    {
        [Fact]
        public void ComparingEqualCollections_ShouldBeEqual()
        {      
            var expected = new TypeWithIEnumerable
              {
                Objects = new List<string> {"test2", "test1"}
              }.ToExpectedObject(ctx => ctx.UseOrdinalComparison());

            var actual = new TypeWithIEnumerable 
              {
                Objects = new List<string> {"test1", "test2"}
              };

            expected.ShouldEqual(actual);
        }
    }
}

After configuring ordinal comparisons for the above test, ExpectedObjects produces the following errors:

Message: System.Exception : The expected object did not match the actual object.

The following issues were found:

1) TypeWithIEnumerable.Objects[0]:

  Expected:
    "test2"
    
  Actual:
    "test1"
    


2) TypeWithIEnumerable.Objects[1]:

  Expected:
    "test1"
    
  Actual:
    "test2"   
Clone this wiki locally