BeEquivalentTo shows enums as decimal in error message
public enum Color
{
Red,Blue
}
Expected member Color to be 1M, but found 0M.
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 });
}
Message: Expected member Color to be Blue, but found Red.
Message: Expected member Color to be 1M, but found 0M.
Running in Xunit, when relevant.
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:
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!
Most helpful comment
Thanks for bringing this up.
The default behavior for comparing
enums inBeEquivalentTois 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());
Message: Expected member Color to be "Blue" with a length of 4, but "Red" has a length of 3.
With configuration:
```
Getting back to your issue, I think the failure message can be improved for both
ComparingEnumsByNameandComparingEnumsByValue.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 = 0withAnotherColor.Red = 1by value.Only displaying
Message: Expected member Color to be Blue, but found Red.would be confusing.The relevant class is
EnumEqualityStep.