I have a test in C# in which I use FluentAssertions to check the result.
[Fact]
public void GetEnabledFeaturesOK()
{
LTAddOnsType[] res = new LTAddOnsType[2];
res [0] = LTAddOnsType.Pro;
res [1] = LTAddOnsType.Live;
res.Should().BeEquivalentTo(new[] {LTAddOnsType.Live, LTAddOnsType.Pro});
}
with an enum like this:
public enum LTAddOnsType : byte
{
Basic = 0,
Pro = 1,
Enterprise = 2,
Live = 4
}
I read that Should().BeEquivalentTo()
by default should compare without strict ordering of the array, but clearly this is not the case, because the test fails, at least for arrays of enums.
What am I missing?
CodePudding user response:
The byte array answer is correct, but rather than changing the underlying enum type, you can simply override FluentAssertions' behaviour here with the following:
[Fact]
public void GetEnabledFeaturesOK()
{
LTAddOnsType[] res = new LTAddOnsType[2];
res [0] = LTAddOnsType.Pro;
res [1] = LTAddOnsType.Live;
res.Should().BeEquivalentTo(
new[] {LTAddOnsType.Live, LTAddOnsType.Pro},
opts => opts.WithoutStrictOrderingFor(_ => true) // Override all ordering expectations
);
}
CodePudding user response:
The error message you get from the failed test case is
Expected res[0] to equal LTAddOnsType.Live {value: 4} by value, but found LTAddOnsType.Pro {value: 1}.
Expected res[1] to equal LTAddOnsType.Pro {value: 1} by value, but found LTAddOnsType.Live {value: 4}.With configuration:
- Use declared types and members
- Compare enums by value
- Compare tuples by their properties
- Compare anonymous types by their properties
- Compare records by their members
- Include all non-private properties
- Include all non-private fields
- Match member by name (or throw)
- Be strict about the order of items in byte arrays
- Without automatic conversion.
This is the default configuration. That second to last entry is the important one.
Because your enum inherits byte, you essentially have a byte array now. Changing your enum to use another data type (such as int) or removing the inheritance will resolve the problem - it will not have strict ordering. Or you can customize the configuration with Tullo's answer.