c# - 设置 ExclusionNestedObjects 时,FluentAssertions 导致 ObjectDisposeException

标签 c# fluent-assertions

我的 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());
}

当我运行它时,测试失败,因为抛出了 ObjectDisposeException 并附带消息:

Safe handle has been closed

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

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

然后测试运行成功。

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

有人遇到过这个吗?有什么解释可以解释为什么我认为应该发生的事情没有发生吗?

最佳答案

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

关于c# - 设置 ExclusionNestedObjects 时,FluentAssertions 导致 ObjectDisposeException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52522480/

相关文章:

c# - RadListView 控件未指定项目占位符

c# - FluentAssertions : ShouldBeEquivalentTo vs Should(). Be() vs Should().BeEquivalentTo()?

c# - 比较时如何使 FluentAssertions ShouldBeEquivalentTo 检查类型?

unit-testing - FluentAssertions ShouldBeEquivalentTo() 与 Should().BeEquivalentTo()

c# - FluentAssertions 应该在生产代码中使用吗?

c# - 流畅的断言 Should().ThrowExactlyAsync 应该对于派生类型失败,但它不会

c# - 在 TFS 中取消 "get latest version"是否有效?

c# - 如何区分系统变量和自定义变量

C# Powershell 管理单元未使用 installutil 注册

c# - 如何在 Umbraco 7 中隐藏属性