I am trying to use the extensibility APIs (IEquivalencyStep) in FluentAssertions to detect if an object recursively contains any property with null value. The object type is unknown at compile time.
I have come up with this:
public static class ObjectAssertionsExtensions
{
public static void BeWithoutNullValues(this ObjectAssertions objectAssertions)
{
objectAssertions.BeEquivalentTo(objectAssertions.Subject, options => options.Using(new NotNullEquivalencyStep()));
}
private class NotNullEquivalencyStep : IEquivalencyStep
{
public EquivalencyResult Handle(Comparands comparands, IEquivalencyValidationContext context, IEquivalencyValidator nestedValidator)
{
comparands.Subject.Should().NotBeNull(context.Reason.FormattedMessage, context.Reason.Arguments);
return EquivalencyResult.ContinueWithNext;
}
}
}
that can be called like this:
instance.Should().BeWithoutNullValues();
But it doesn't really work as expected - I think one of the default equivalency steps make the test pass when it should fail because I am using the subject as expectation (they are equal by reference).
However, I have found that if I modify the global equivalency assertion options, It works:
public static void BeWithoutNullValues(this ObjectAssertions objectAssertions)
{
AssertionOptions.AssertEquivalencyUsing(options => options.Using(new NotNullEquivalencyStep()));
objectAssertions.BeEquivalentTo(objectAssertions.Subject);
}
Modifying the global options is not an acceptable solution, so I have two questions:
- Is it possible to disable the default equivalency steps without modifying the global options?
- If not, is it possible to reset the global options afterwards (i.e. de-registering
NotNullEquivalencyStep)?