[英]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.