It’s very common that we assert if two objects are equivalent in unit tests. Using FluentAssertion, this looks like the following
actual.ShouldBeEquivalentTo(expected);
However, there are times where you want to exclude certain properties when comparing objects. This might be because the value of the property cannot be predicted or you just don’t want to include that property in the assertion. FluentAssert provides an overload method ShouldBeEquivalentTo that allows you to specify which property to exclude.
actual.ShouldBeEquivalentTo(expected, options => { options.Excluding(info => info.UniqueId); options.Excluding(info => info.Created); options.Excluding(info => info.Updated); return options; });
And to exclude certain properties when asserting if two arrays of object are equivalent, you can do the following
expected.ShouldAllBeEquivalentTo(actual, options => { options.Excluding(info => info.UniqueId); options.Excluding(info => info.Created); options.Excluding(info => info.Updated); return options; });