2

我有一个单元/集成测试如下。

using (var repository = _factory.Get())
{
    applicationBefore = repository.Applications
        .Include(a => a.AcceptedAgreements)
        .Single(a => a.AggregateId == applicationId);
}

// Perform an operation that deletes an Application and all
// related data and then rebuilds it from the event store.

Repo.Application applicationAfter = null;

using (var repository = _factory.Get())
{
    applicationAfter = repository.Applications
        .Include(a => a.AcceptedAgreements)
        .Single(a => a.AggregateId == applicationId);
}

applicationAfter.AcceptedAgreements.ShouldAllBeEquivalentTo(applicationBefore.AcceptedAgreements, options => options
    .ExcludingNestedObjects());

repository引用是一个并且是一个DbContext导航AcceptedAgreements属性。

此测试失败并显示以下消息。

Result Message: 
FluentAssertions.Execution.AssertionFailedException : 
Expected item[0].Application.Id to be 1, but found 2.
<more failures where stuff inside Application is different>

With configuration:
- Use declared types and members
- Compare enums by value
- Include all non-private properties
- Include all non-private fields
- Match member by name (or throw)
- Be strict about the order of items in byte arrays
- FluentAssertions.Equivalency.ShouldAllBeEquivalentToHelper+CollectionMemberOrderingRuleDecorator

如果我修改断言如下:

applicationAfter.AcceptedAgreements.ShouldAllBeEquivalentTo(applicationBefore.AcceptedAgreements, options => options
    .Excluding(o => o.Application));

现在测试通过了。

请帮助我理解为什么ExcludingNestedObjects()不排除该Application属性,它实际上是一个嵌套对象,我不得不单独排除每个导航属性。上面的代码稍微简化了,因为我实际上有多个导航属性,并且必须单独排除它们中的每一个。

4

0 回答 0