Skip to content

WithAutoConversion throw ArgumentException for different enums #2259

@jnyrup

Description

@jnyrup

Description

With #2147 we now try to convert the subject into an enum if the expectation is an enum.
This does not work when the subject is of another enum type.

Reproduction Steps

[Fact]
public void Test()
{
    var subject = new { EnumA.Value };
    var expected = new { EnumB.Value };
    subject.Should().BeEquivalentTo(expected, o => o.WithAutoConversion());
}

enum EnumA { Value }

enum EnumB { Value }

Expected behavior

Test should pass

Actual behavior

Message: 
System.ArgumentException : Object must be the same type as the enum. The type passed in was 'TestProject19.UnitTest1+EnumA'; the enum type was 'TestProject19.UnitTest1+EnumB'.

  Stack Trace: 
RuntimeType.IsEnumDefined(Object value)
Enum.IsDefined(Type enumType, Object value)
AutoConversionStep.TryChangeType(Object subject, Type expectationType, Object& conversionResult)
AutoConversionStep.Handle(Comparands comparands, IEquivalencyValidationContext context, IEquivalencyValidator nestedValidator)
EquivalencyValidator.TryToProveNodesAreEquivalent(Comparands comparands, IEquivalencyValidationContext context)
EquivalencyValidator.RecursivelyAssertEquality(Comparands comparands, IEquivalencyValidationContext context)
StructuralEqualityEquivalencyStep.AssertMemberEquality(Comparands comparands, IEquivalencyValidationContext context, IEquivalencyValidator parent, IMember selectedMember, IEquivalencyAssertionOptions options)
StructuralEqualityEquivalencyStep.Handle(Comparands comparands, IEquivalencyValidationContext context, IEquivalencyValidator nestedValidator)
EquivalencyValidator.TryToProveNodesAreEquivalent(Comparands comparands, IEquivalencyValidationContext context)
EquivalencyValidator.RecursivelyAssertEquality(Comparands comparands, IEquivalencyValidationContext context)
EquivalencyValidator.AssertEquality(Comparands comparands, EquivalencyValidationContext context)
ObjectAssertions`2.BeEquivalentTo[TExpectation](TExpectation expectation, Func`2 config, String because, Object[] becauseArgs)
UnitTest1.Fails() line 13

Regression?

Yes, introduced with 6.11

Known Workarounds

No response

Configuration

FluentAssertions 6.11.0

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions