在可空枚举上设置 FluentAssertions 期望

Setting FluentAssertions Expectation on Nullable Enum

我有以下对象(为简洁起见简化为单个 属性)

public class MyObject {
    public string MyProperty { get; set; }
}

和对应的DTO

public class MyDto {
    public MyEnum? MyProperty {get; set;}
}

我的映射设置为当字符串 MyProperty 为空时,枚举设置为空。现在我想使用 FluentAssertions 对此进行测试(还有很多其他属性),所以我尝试使用以下

设置等效比较行为
myDto.ShouldBeEquivalentTo(myObject, opt => opt.Using<MyEnum?>(ctx => ctx.Subject.Should().Be(null)).When(info => info.SelectedMemberPath.Equals(nameof(MyDto.MyProperty))));

但这不起作用,因为我遇到了异常

Expected member MyProperty to be a System.String, but found a System.Nullable

我做错了什么?

我认为您在这里发现了一个边缘案例。 FluentAssertions 将 enumsstrings 视为特殊的,但不处理 nullable enums。 Using/WhenTypes 不支持处理不同类型的属性。解决该问题的唯一方法是提交变更请求。