1

Writing some integration tests we want to compare values read from the DB (int) against the object being used (enum field on a DTO).

So conceptually dto.EnumValue.Should().Be((MyEnum)expectedIntValue)

However these are both nullable. I know how to compare two nullable values of the same type but I can't see an obvious way to compare nullable int against nullable enum.

So far we ended up with:

if(dto.EnumValue == null)
 expectedValue.Should().BeNull();
else
 dto.EnumValue.Should().Be((MyEnum)expectedIntValue.Value);

I feel there is surely a way to do this better, adding logic to unit test expectations is not great?

Mr. Boy
  • 60,845
  • 93
  • 320
  • 589

1 Answers1

1

You need to convert one value to match the datatype of the other. So either:

enumValue.Should().Be((Foo?)intValue);

Or:

((int?)enumValue).Should().Be(intValue);
DavidG
  • 113,891
  • 12
  • 217
  • 223