简体   繁体   English

断言一个对象集合是另一个相同类型对象集合的一部分

[英]Asserting a collection of objects is a part of another collection of objects of the same type

I'm trying to use .BeSubsetOf() from Fluent Assertions to ensure that one collection of objects is a subset of another, bigger collection of objects of the same type, but the assertion always fails.我正在尝试使用 Fluent Assertions 中的.BeSubsetOf()来确保一个对象集合是另一个更大的相同类型对象集合的子集,但断言总是失败。

Example code:示例代码:

    var expected = new[]
    {
        new Campus
        {
            Id = 1,
            Name = "Campus 1"
        },

        new Campus
        {
            Id = 2,
            Name = "Campus 2"
        },

        new Campus
        {
            Id = 3,
            Name = "Campus 3"
        }
    };

    var actual = new[]
    {
        new Campus
        {
            Id = 1,
            Name = "Campus 1"
        },

        new Campus
        {
            Id = 2,
            Name = "Campus 2"
        }
    };

    actual.Should().BeSubsetOf(expected);

What's wrong here?这里有什么问题?

That is happening because BeSubsetOf use the object's .Equals method for comparison.这是因为BeSubsetOf使用对象的.Equals方法进行比较。

One option is to override .Equals method of the type you are comparing.一种选择是覆盖您正在比较的类型的.Equals方法。

Second alternative can be verifying one object at the time第二种选择可以同时验证一个 object

foreach (var campus in actual)
{
   expected.Should().ContainEquivalentOf(campus);
}

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

相关问题 将对象集合添加到另一个集合 - Adding collection of objects to another collection 展平并将对象内部集合映射到另一个集合 - Flatten and map objects inner collection to another collection 对另一个对象集合中的对象集合进行排序 - Sorting Collections of Objects within another collection of Objects 实体框架:更新实体与同类型对象集合的关系 - Entity Framework: Update entity with relationship to collection of objects of same type 通用约束,用于限制集合具有相同类型的派生对象 - generic constraint to restrict collection to have same type of derived objects WPF绑定到集合中多个对象的相同属性 - WPF binding to the same property of multiple objects in a collection 如何在同一对象集合上获取多个枚举器? - How to get Multiple Enumerator on same Collection of objects? 将通用对象集合传递给需要基类型集合的方法 - Passing a generic collection of objects to a method that requires a collection of the base type 比较引用类型对象的集合是否相等,忽略集合中项目的顺序 - Comparing a collection of reference type objects for equality ignoring order of items in collection 如何用新对象填充集合,从另一个集合复制属性 - How to populate a collection with new objects, copying properties from another collection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM