【发布时间】:2014-11-12 02:46:45
【问题描述】:
我想知道是否有办法比较两个不同类型的列表。我找到了这个方法:
public AndConstraint<TAssertions> Equal(IEnumerable<T> expectation, Func<T, T, bool> predicate, string because = "", params object[] reasonArgs)
{
this.AssertSubjectEquality<T>((IEnumerable) expectation, predicate, because, reasonArgs);
return new AndConstraint<TAssertions>((TAssertions) this);
}
我正在寻找类似的东西:
public AndConstraint<TAssertions> Equal<U>(IEnumerable<T> expectation, Func<T, U, bool> predicate, string because = "", params object[] reasonArgs)
{
this.AssertSubjectEquality<T,U>((IEnumerable) expectation, predicate, because, reasonArgs);
return new AndConstraint<TAssertions>((TAssertions) this);
}
我尝试制作扩展方法,但方法 AssertSubjectEquality 受到保护,不支持第二种类型。
【问题讨论】:
-
不确定我是否完全理解您想要什么。您有两个不同泛型类型的不同列表,并且您想针对每个条目按顺序测试谓词?所以也许是这样的?
list1.Zip(list2, (a, b) => new { A = a, B = b }).Should().Equal(zippedInput => zippedInput.A == "something" && zippedInput.B == "somethingElse");?除此之外,由于 FluentAssertions 是开源的,您可以随时下载并添加到其中以供您使用。我已经完成了这项工作,并且没有遇到任何重大问题。 -
你明白了,你的代码稍作修改就可以工作
list.Zip(monitoringObjects, (a, b) => new { A = a, B = b }).Should().Contain(x=> x.A.Id == x.B.Id);虽然它不是很有效,因为我使用的是Contain,它适用于无序列表。也许@DennisDomen 可能有一个建议。
标签: c# fluent-assertions