断言一个对象集合是另一个相同类型对象集合的一部分
Asserting a collection of objects is a part of another collection of objects of the same type
我正在尝试使用 Fluent Assertions 中的 .BeSubsetOf()
来确保一个对象集合是另一个更大的相同类型对象集合的子集,但断言总是失败。
示例代码:
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);
这是怎么回事?
That is happening because BeSubsetOf
use the object's
.Equals
method for comparison.
一种选择是重写您正在比较的类型的 .Equals
方法。
第二种选择可以是同时验证一个对象
foreach (var campus in actual)
{
expected.Should().ContainEquivalentOf(campus);
}
我正在尝试使用 Fluent Assertions 中的 .BeSubsetOf()
来确保一个对象集合是另一个更大的相同类型对象集合的子集,但断言总是失败。
示例代码:
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);
这是怎么回事?
That is happening because
BeSubsetOf
use the object's.Equals
method for comparison.
一种选择是重写您正在比较的类型的 .Equals
方法。
第二种选择可以是同时验证一个对象
foreach (var campus in actual)
{
expected.Should().ContainEquivalentOf(campus);
}