简体   繁体   English

流畅的断言应该AllBeEquivalentTo

[英]Fluent Assertions ShouldAllBeEquivalentTo

I'm not sure if an old version of FluentAssertions had this or not but I'd like to compare a collection to another collection.我不确定旧版本的 FluentAssertions 是否有这个,但我想将一个集合与另一个集合进行比较。 I have a dto like so:我有一个像这样的dto:

public class UserDTO
{
    public int Id { get; set; }
    public string Username { get; set; }
}

I have two lists Id like to compare.我有两个我想比较的列表。

 List<UserDTO> createdUsers = this.GetCreatedUser();
 var expectedResults = this.dbContext.Users.Top(10);

The closest thing I see to should all be equivalent is:我看到的最接近的应该都是等价的:

 createdUsers.Should().AllBeEquivalentTo(expectedResults)

but when I try to pass my exclusions, it seems to be operating providing me exlusions for the list instead of the entity itself.但是当我尝试通过排除项时,它似乎在为我提供列表的排除项而不是实体本身。

I would like to compare two list of these excluding the Id property.我想比较其中的两个列表,不包括 Id 属性。 I could of sworn there was a function called ShouldAllBeEquivalentTo which took in options to allow exluding,我可以发誓有一个名为ShouldAllBeEquivalentTo的函数,它接受了允许ShouldAllBeEquivalentTo选项,

createdUsers.ShouldAllBeEquivalentTo(expectedResults, o => o.Excluding(x => x.Id);

How can I compare collections while excluding properties in the comparison?如何在比较集合的同时排除比较中的属性?

Documentation suggests the following when it comes to exclusions with Collections and Dictionaries当涉及到集合和字典的排除时,文档建议如下

createdUsers.Should().BeEquivalentTo(expectedResults, options => options.Excluding(_ => _.Id));

Quote from documentation:引用自文档:

to assert that all instances of OrderDto are structurally equal to a single object:断言 OrderDto 的所有实例在结构上都等于单个对象:

orderDtos.Should().AllBeEquivalentTo(singleOrder);

Reference Object graph comparison: Collections and Dictionaries参考对象图比较:集合和字典

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM