Skip to content

AssertEquivalencyUsing support for unbound generic types #1388

@schnerring

Description

@schnerring

Description

I came across this question on Stack Overflow where someone uses Optional in combination with FA.

Optional provides a generic struct Option<T> that overrides Equals, so FA assumes value semantics.

Now if one would want to override this behavior globally, so Option<T> uses ComparingByMembers for any T, one had to add a call to AssertionOptions.AssertEquivalencyUsing for each T.

A (simplistic) workaround for this is an extension method like this:

public static class FluentAssertionsExtensions
{
    public static void BeEquivalentByMembers<TExpectation>(
        this ComparableTypeAssertions<TExpectation> actual,
        TExpectation expectation)
    {
        actual.BeEquivalentTo(
            expectation,
            options => options.ComparingByMembers<TExpectation>());
    }
}

If an override that supports a Type argument existed, the following would be possible:

AssertionOptions.AssertEquivalencyUsing(options => options.ComparingByMembers(typeof(Option<>))

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions