Asserting object graph equivalence using Fluent Assertions 2.0
Selecting the right properties
orderDto.ShouldBeEquivalentTo(order);
In contrast to the ShouldHave() extension method, the comparison is recursive by default and all properties of the OrderDto must be available on the Order. If not, an exception is thrown. You can override this behavior in different ways. For instance, you may only want to include the properties both object graphs have:
orderDto.ShouldBeEquivalentTo(order, options =>
options.ExcludingMissingProperties());
You can also exclude certain (potentially deeply nested) properties using the Excluding() method.
orderDto.ShouldBeEquivalentTo(order, options =>
options.Excluding(o => o.Customer.Name));
Obviously, Excluding() and ExcludingMissingProperties() can be combined. Maybe farfetched, but you may even decide to exclude a property on a particular nested object by its index.
orderDto.ShouldBeEquivalentTo(order, options =>
options.Excluding(o => o.Products[1].Status));
The Excluding() method on the options object also takes a lambda expression that offers a bit more flexibility for deciding what property to include.
orderDto.ShouldBeEquivalentTo(order, options => options
This expression has access to the property path, the property info and the subject’s run-time and compile-time type. You could also take a different approach and explicitly tell FA which properties to include.
orderDto.ShouldBeEquivalentTo(order, options => options
Overriding and collections
orderDto.ShouldBeEquivalentTo(order, options => options
If you want to do this for all properties of a certain type, you can shorten the above call like this.
orderDto.ShouldBeEquivalentTo(order, options => options
The original ShouldHave() extension method does support collections now, but it doesn’t allow you to influence the comparison based on the actual collection type. The new extension method ShouldAllBeEquivalentTo() does support that so you can now take the 2nd example from the post and apply it on a collection of OrderDtos.
orderDtos.ShouldAllBeEquivalentTo(orders, options =>
options.Excluding(o => o.Customer.Name));
Extensibility
- Select the properties of the subject object to include in the comparison.
- Find a matching property on the expectation object and decide what to do if it can’t find any.
- Select the appropriate assertion method for the property’s type and execute it.
- Choose the appropriate phase that the rule should influence
- Select the corresponding interface
- Create a class that implements this interface
- Add it to the ShouldBeEquivalentTo() call using the Using() method on the options parameters.
That’s it for now. As usual, for questions, remarks or suggestions, you can use the Discussions page, StackOverflow, or you can contact me directly by email or Twitter.
Off topic: Although we’re still on CodePlex, the source code of Fluent Assertions is now stored in a Git repository. That should make it a whole lot easier for contributors to fork the code, write a nice improvement, and send us a pull request.
Leave a Comment