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.

Class with Enum Property .Should().BeEquivalentTo Class with string Property does not work - Fluent Assertions 6.0.0/6.1.0

See original GitHub issue

Description

With Fluent Assertion 6.0.0 I found no way that .Should().BeEquivalentTo can compare a enum and a string property in an object graph.

With Fluent Assertion 5.4.x it’s working when I specify this option: config => config.ComparingEnumsByName()

As far as I understand the documentation and the upgrade guide, this still should work, am I right? https://fluentassertions.com/upgradingtov6 https://www.continuousimprover.com/2021/08/fluent-assertions-v6

Example to reproduce the issue

public class ObjectWithEnum
{
    public State State
    {
        get;
        set;
    }
}

public class ObjectWithString
{
    public string State
    {
        get;
        set;
    }
}

public enum State
{
    Init,
    Done
}

// Arrange
ObjectWithEnum objectWithEnum = new ObjectWithEnum{ State = State.Init};
ObjectWithString objectWithString = new ObjectWithString { State = State.Init.ToString()};

// Act && Assert
objectWithString.Should().BeEquivalentTo(objectWithEnum, config => config.ComparingEnumsByName());

Expected behavior:

I would expect that my test is green.

Actual behavior:

My test is red, with the following error:

Expected property root.State to be equivalent to State.Init {value: 0}, but found “Init”.

With configuration:

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

Versions

This happens with version 6.0.0 and with the latest version (6.1.0).

We are using .Net 5.x, but it also happens on .net 4.x.

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
jnyrupcommented, Aug 25, 2021

You’re missing the sentence above the code snippet

The practical implications are that the following examples now fails.

var subject = new { Value = "One" };
var expectation = new { Value = MyOtherEnum.One };
subject.Should().BeEquivalentTo(expectation,  opt => opt.ComparingEnumsByName());

var subject = new { Value = 1 };
var expectation = new { Value = MyOtherEnum.One };
subject.Should().BeEquivalentTo(expectation,  opt => opt.ComparingEnumsByValue());

ComparingEnumsByName is used to control how to compare two enums of different type.

I haven’t given it a lot of thought if it’s the best workaround, but here’s one way

objectWithString.Should().BeEquivalentTo(objectWithEnum, config => config
    .Using<object>(ctx => ctx.Subject.Should().Be(ctx.Expectation.ToString()))
    .When(e => e.RuntimeType == typeof(State)));
0reactions
rizicommented, Aug 25, 2021

Ah yes. So we can close it?

Wondering what happens if you enable auto-conversion.

Yes, we can close the issue.

@AutoConversion: That did not change the behaviour (the test fails with the same error). I also tried to combine the following config => config.WithAutoConversion().ComparingEnumsByName()), but this also still results in the same error.

Read more comments on GitHub >

github_iconTop Results From Across the Web

FluentAssertions Should().BeEquivalentTo() fails in trivial ...
I'm trying to do the simplest thing imaginable: compare the properties of an Address object with the properties of an AddressDto object. They ......
Read more >
Upgrading to version 6.0 - Fluent Assertions
This approach did not work well for enums as it would consider two enums to be equal if their underlying integral values are...
Read more >
Fluent Assertions 5.0: The best unit test assertion library in the ...
First of all, the equivalency algorithm will now use the expectation to drive the comparison. For years, it would use the properties and/or ......
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