Class with Enum Property .Should().BeEquivalentTo Class with string Property does not work - Fluent Assertions 6.0.0/6.1.0
See original GitHub issueDescription
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:
- Created 2 years ago
- Comments:5 (3 by maintainers)
Top GitHub Comments
You’re missing the sentence above the code snippet
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
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.