Search code examples
c#unit-testingfluent-assertions

FluentAssertions : Should().BeEquivalentTo fails for un-ordered arrays of enums


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?


Solution

  • 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
      );
    }