question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

BeEquivalentTo shows enums as decimal in error message

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
jnyrupcommented, Aug 20, 2018

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

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.

0reactions
robvanudencommented, Sep 29, 2018

Agree, thanks @krajek!

Read more comments on GitHub >

github_iconTop Results From Across the Web

FluentAssertions Should().BeEquivalentTo() fails with Lists ...
c# - FluentAssertions Should(). BeEquivalentTo() fails with Lists containing run-time specified types deriving from identical interface - Stack ...
Read more >
Upgrading to version 6.0 - Fluent Assertions
BeEquivalentTo (expectation, opt => opt.ComparingEnumsByValue());. If your assertions rely on the formatting of enums in failure messages, you'll notice that ...
Read more >
How to declare enums with float value.
Hi, I'm trying to make and enum that contains informations about the rarity of a item and i'm using enums, but if make...
Read more >
Types — Solidity 0.8.20 documentation
Types . Solidity is a statically typed language, which means that the type of each variable (state and local) needs to be...
Read more >
Message (Java(TM) EE 7 Specification APIs)
The Message interface is the root interface of all JMS messages. ... An exact numeric literal is a numeric value without a decimal...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found