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

Contain fails to find enum value in 6.1.0 #1697

Closed
luiscantero opened this issue Oct 3, 2021 · 2 comments
Closed

Contain fails to find enum value in 6.1.0 #1697

luiscantero opened this issue Oct 3, 2021 · 2 comments

Comments

@luiscantero
Copy link

luiscantero commented Oct 3, 2021

I'm unsure if this is a bug in FluentAssertions, and I went through the migration notes regarding Enums. I'm reporting it just in case. Note that the original code that lead to this issue is not as obvious as this one (e.g. the convertion to ushort) - this is just a simple repro.

With 5.10.3, the following code executed without problems:

var dict = new Dictionary<string, object>
{
    ["/EventId"] = "SomeId",
    ["/Severity"] = (ushort)Severity.High,
};

dict.Should().Contain(new Dictionary<string, object>
{
    ["/EventId"] = "SomeId",
    ["/Severity"] = Severity.High,
});

After upgrading to 6.1.0, we get this exception:
FluentAssertions.Execution.AssertionFailedException: 'Expected dict to contain value Severity.High {value: 1} at key "/Severity", but found 1us.'

To solve the issue, the following works:

dict.Should().Contain(new Dictionary<string, object>
{
    ["/EventId"] = "SomeId",
    ["/Severity"] = (int)Severity.High,
});

Environment:
.NET Core 3.1, VS2019, Windows.

@jnyrup
Copy link
Member

jnyrup commented Oct 3, 2021

This is by design and part of
https://github.com/fluentassertions/fluentassertions/blob/master/docs/_pages/releases.md#600

Major overhaul on how enums are handled, see the Migration Guide for more details - #1479.

Consider this case, which passed in 5.10.3

enum Severity
{
    Low,
    Medium,
    High
}

enum Color
{
    Red,
    Green,
    Blue
}

[TestMethod]
public void Lollostein()
{
    var subject = new Dictionary<string, object>
    {
        ["/EventId"] = "SomeId",
        ["/Severity"] = Severity.High,
    };

    var expected = new Dictionary<string, object>
    {
        ["/EventId"] = "SomeId",
        ["/Severity"] = Color.Blue,
    };

    subject.Should().Contain(expected);
}

To retain the old behavior your workaround seems fine to me, as it's more explicit in that you want to compare the underlying values.

@luiscantero
Copy link
Author

Thanks for the info, pls feel free to close this issue.

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

No branches or pull requests

3 participants