我试图断言Claim集合包含一组预期的声明。我似乎遇到的问题是,没有办法检查子集并提供我自己的等价选项。
var expected = new[] {
new Claim(ClaimTypes.Name, "joshdev"),
new Claim(ClaimTypes.Email, "test@test.com"),
new Claim(ClaimTypes.GivenName, "Josh"),
new Claim(ClaimTypes.Surname, "Perry"),
};
var identity = GetIdentity();我试过的..。
identity.Claims.ShouldAllBeEquivalentTo(expected, options => options.Including(x => x.Type).Including(x => x.Value));如果身份的声明不完全是预期的集合,则此操作将失败,例如,不仅仅是这些声明。
identity.Claims.Should().Contain(expected);这会失败,因为Contain只是使用了Claim类型没有实现的object::Equals方法。
我需要的是一些方法来做Contain,但与ShouldAllBeEquivalentTo公开的具有相同的等价选项。我认为ShouldBeEquivalentTo可能是我想要的,但它提供了对集合对象本身的断言,而不是集合中的项。
发布于 2012-12-19 00:45:49
不幸的是,这在当前版本中还不可能实现。扩展点已经存在(请参见EquivalencyValidator类),但是步骤列表当前是私有的。否则,您可以将EnumerableEquivalencyStep替换为提供“包含”行为的您自己的实现。
发布于 2017-11-14 23:14:33
如果您使用Shouldly,则可以将ShouldAllBe与包含一起使用。
collection1 = {1, 2, 3, 4};
collection2 = {2, 4, 1, 3};
collection1.ShouldAllBe(item=>collection2.Contains(item)); // true最后,您可以编写一个扩展。
public static class ShouldlyIEnumerableExtensions
{
public static void ShouldEquivalentTo<T>(this IEnumerable<T> list, IEnumerable<T> equivalent)
{
list.ShouldAllBe(l => equivalent.Contains(l));
}
}更新
collection1.ShouldBe(collection2, ignoreOrder: true); // truehttps://stackoverflow.com/questions/13921808
复制相似问题