Home > Mobile >  FluentAssertions - Compare equivalency where properties have different names?
FluentAssertions - Compare equivalency where properties have different names?

Time:11-05

I'm using FluentAssertions to compare equivalency of objects after mapping:

x.Should().BeEquivalentTo(y);

It's not uncommon for x to have a property which is named slightly different to y, in which case it would be a great to be able to tell FluentAssertions that the values of these 2 properties should match. Ideally with something like:

(y => y.PropertyWithName, x.PropertyWithVerySimilarName)

I'm reading the docs, but I can't see a clear way to do this. Has anyone found a way? https://fluentassertions.com/objectgraphs/

CodePudding user response:

I would make use of Tuples here.

Just pick up set of properties to be "equivalent by":

var actual = x
    .Select(item => (item.Prop1, item.Prop2, item.Prop3))
    .ToArray();
var expected = y
    .Select(item => (item.Property1, item.SpecialProp, item.Prop3))
    .ToArray();
actual.Should().BeEquivalentTo(expected);

Just remember that the order of properties in tuple matters.

CodePudding user response:

This feature is actively in development: https://github.com/fluentassertions/fluentassertions/issues/535

  • Related