Fluentassertions: BeEquivalentTo shows enums as decimal in error message

Created on 18 Aug 2018  路  5Comments  路  Source: fluentassertions/fluentassertions

Description

BeEquivalentTo shows enums as decimal in error message

public enum Color
{
    Red,Blue
}

Expected member Color to be 1M, but found 0M.

Complete minimal example reproducing the issue

public enum Color
{
    Red,Blue
}

public class Car
{
    public Color Color { get; set; }
}

[Fact]
public void Test()
{
    var car = new Car
    {
        Color = Color.Red
    };
    car.Should().BeEquivalentTo(new Car() { Color = Color.Blue });
}

Expected behavior:

Message: Expected member Color to be Blue, but found Red.

Actual behavior:

Message: Expected member Color to be 1M, but found 0M.

Versions

  • FluentAssertions 5.4.1
  • .NET Core 2.0

Additional Information

Running in Xunit, when relevant.

enhancement help wanted

Most helpful comment

Thanks for bringing this up.

The default behavior for comparing enums in BeEquivalentTo is to compare them by value.
That is, two (different) enums are considered equivalent if and only if their underlying values are equal.

If you want to compare the enums by name instead, you can use

```c#
car.Should().BeEquivalentTo(new Car() { Color = Color.Blue },
opt => opt.ComparingEnumsByName());


which changes the failure message to

Message: Expected member Color to be "Blue" with a length of 4, but "Red" has a length of 3.

With configuration:

  • Use declared types and members
  • Compare enums by name
  • Match member by name (or throw)
  • Without automatic conversion.
  • Be strict about the order of items in byte arrays
    ```

Getting back to your issue, I think the failure message can be improved for both ComparingEnumsByName and ComparingEnumsByValue.

Some ideas for the failure message:

  • Expected member Color to be Color.Blue, but found Color.Red.
  • Expected member Color to be Blue (1), but found Red (0).
  • Expected member Color to be Color.Blue (1), but found Color.Red (0).

Including more details is useful if one e.g. compares Color.Red = 0 with AnotherColor.Red = 1 by value.
Only displaying Message: Expected member Color to be Blue, but found Red. would be confusing.

The relevant class is EnumEqualityStep.

All 5 comments

Thanks for bringing this up.

The default behavior for comparing enums in BeEquivalentTo is to compare them by value.
That is, two (different) enums are considered equivalent if and only if their underlying values are equal.

If you want to compare the enums by name instead, you can use

```c#
car.Should().BeEquivalentTo(new Car() { Color = Color.Blue },
opt => opt.ComparingEnumsByName());


which changes the failure message to

Message: Expected member Color to be "Blue" with a length of 4, but "Red" has a length of 3.

With configuration:

  • Use declared types and members
  • Compare enums by name
  • Match member by name (or throw)
  • Without automatic conversion.
  • Be strict about the order of items in byte arrays
    ```

Getting back to your issue, I think the failure message can be improved for both ComparingEnumsByName and ComparingEnumsByValue.

Some ideas for the failure message:

  • Expected member Color to be Color.Blue, but found Color.Red.
  • Expected member Color to be Blue (1), but found Red (0).
  • Expected member Color to be Color.Blue (1), but found Color.Red (0).

Including more details is useful if one e.g. compares Color.Red = 0 with AnotherColor.Red = 1 by value.
Only displaying Message: Expected member Color to be Blue, but found Red. would be confusing.

The relevant class is EnumEqualityStep.

Didn't know about opt => opt.ComparingEnumsByName(), but what you're saying all makes sense. Thanks for the information.

@dennisdoomen can we close this one?

If @robvanuden agrees, then yes?

Agree, thanks @krajek!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dennisdoomen picture dennisdoomen  路  3Comments

julealgon picture julealgon  路  3Comments

felpel picture felpel  路  4Comments

shift-evgeny picture shift-evgeny  路  5Comments

jnyrup picture jnyrup  路  3Comments