【问题标题】:FluentAssertions causing ObjectDisposedException when ExcludingNestedObjects is set设置 ExcludingNestedObjects 时导致 ObjectDisposedException 的 FluentAssertions
【发布时间】:2019-03-02 12:17:36
【问题描述】:

我有一个 EF 模型定义如下:

public class ProgramManagerRolePermission
{
    [Key, Column(Order = 1)]
    public Guid ShardKey { get; set; }

    [Key, Column(Order = 2)]
    public Guid RoleId { get; set; }

    [Key, Column(Order = 3)]
    public Guid PermissionId { get; set; }

    [ForeignKey(nameof(RoleId))]
    public virtual ProgramManagerRole Role { get; set; }

    [ForeignKey(nameof(PermissionId))]
    public virtual ProgramManagerPermission Permission { get; set; }
}

我有以下测试代码:

using (var repo = IdentityProgramRepositoryFactory.Get())
{
    var role = repo.ProgramManagerRoles
        .Include(r => r.Permissions)
        .SingleOrDefault(r => r.Id == roleId);
    role.Permissions.Should().BeEquivalentTo(new[] {
        new Repo.ProgramManagerRolePermission
        {
            PermissionId = ProgramManagerPermissions.GrantOrRevokeRoles.Id,
            RoleId = roleId,
            ShardKey = identityProgramId
        },
        new Repo.ProgramManagerRolePermission
        {
            PermissionId = ProgramManagerPermissions.ManageNode.Id,
            RoleId = roleId,
            ShardKey = identityProgramId
        }
    }, options => options.ExcludingNestedObjects());
}

当我运行它时,测试失败,因为ObjectDisposedException 被抛出消息:

安全手柄已关闭

如果我将最后一行更改为:

}, options => options.Excluding(p => p.Role).Excluding(p => p.Permission));

那么测试运行成功。

仅有的两个嵌套对象是RolePermission。当我明确排除它们时,测试有效,当我告诉它排除所有嵌套对象时,它似乎仍在尝试浏览它们。

以前有人遇到过这种情况吗?关于为什么我认为应该发生的事情没有任何解释?

【问题讨论】:

    标签: c# fluent-assertions


    【解决方案1】:

    您正在使用ExcludingNestedObjects,这意味着它不会在RolePermission 对象公开的对象之间进行结构比较。这些是您的根对象的属性。但它仍然会尝试做一个简单的相等检查。

    【讨论】:

    • 我误解了智能感知告诉我ExcludingNestedObjects 的内容。我错误地将其解释为本质上意味着它将完全跳过任何作为引用类型的属性。感谢您清理它。
    • 好吧,我知道你可能会误解这一点。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-28
    • 1970-01-01
    • 2018-04-16
    • 2011-06-23
    • 2011-07-03
    相关资源
    最近更新 更多