Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WithAutoConversion throw ArgumentException for different enums #2259

Closed
jnyrup opened this issue Aug 15, 2023 · 0 comments · Fixed by #2261
Closed

WithAutoConversion throw ArgumentException for different enums #2259

jnyrup opened this issue Aug 15, 2023 · 0 comments · Fixed by #2261
Labels

Comments

@jnyrup
Copy link
Member

jnyrup commented Aug 15, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant