Skip to content

Ignoring Members

Derek Greer edited this page Jan 5, 2021 · 4 revisions

ExpectedObjects provides three approaches to ignoring members when performing strongly-typed comparisons: an Ignore() method which accepts a member expression, an IgnoreRelativePath() method, and an IgnoreAbsolutePath() method which accept relative and absolute member paths respectively.

The following example demonstrates how to ignore a member by supplying a member expression:

using ExpectedObjects;
using Xunit;

namespace ExpectedObjectExamples.Specs
{
    public class CustomerSpecs
    {
        [Fact]
        public void ComparingEqualCustomers_ShouldBeEqual()
        {
            // establish context
            var expectedCustomer = new Customer
            {
                FirstName = "Silence",
                LastName = "Dogood",
                Address = new Address
                {
                    AddressLineOne = "The New-England Courant",
                    City = "Boston",
                    State = "MA",
                    PostalCode = "02114"
                }
            }.ToExpectedObject(ctx => ctx.Ignore(x => x.Address.AddressLineTwo));

            var actualCustomer = new Customer
            {
                FirstName = "Silence",
                LastName = "Dogood",
                Address = new Address
                {
                    AddressLineOne = "The New-England Courant",
                    AddressLineTwo = "3 King Street",
                    City = "Boston",
                    State = "MA",
                    PostalCode = "02114"
                }
            };


            // observation
            expectedCustomer.ShouldEqual(actualCustomer);
        }
    }
}

This same behavior can be achieved by specifying the absolute member path as a string:

using ExpectedObjects;
using Xunit;

namespace ExpectedObjectExamples.Specs
{
    public class CustomerSpecs
    {
        [Fact]
        public void ComparingEqualCustomers_ShouldBeEqual()
        {
            // establish context
            var expectedCustomer = new Customer
            {
                FirstName = "Silence",
                LastName = "Dogood",
                Address = new Address
                {
                    AddressLineOne = "The New-England Courant",
                    City = "Boston",
                    State = "MA",
                    PostalCode = "02114"
                }
            }.ToExpectedObject(ctx => ctx.IgnoreAbsolutePath("Customer.Address.AddressLineTwo"));

            var actualCustomer = new Customer
            {
                FirstName = "Silence",
                LastName = "Dogood",
                Address = new Address
                {
                    AddressLineOne = "The New-England Courant",
                    AddressLineTwo = "3 King Street",
                    City = "Boston",
                    State = "MA",
                    PostalCode = "02114"
                }
            };


            // observation
            expectedCustomer.ShouldEqual(actualCustomer);
        }
    }
}

In cases where you want to ignore all properties with the name Id, you can provide a relative path denoting only the matching member name to ignore:

            var expectedAggregateRoot = new AggregateRoot
            {
                ChildEntity = new ChildEntity
                {
                    PropertyOne = "Value1",
                    PropertyTwo = "Value2",
                }
            }.ToExpectedObject(ctx => ctx.IgnoreRelativePath("Id"));

Assuming AggregateRoot and ChildEntity both have Id members, the values for each member within the actual object will be ignored.

For cases where you need to ignore many fields, consider using Partial-Comparisons.

Clone this wiki locally