给定一些AssertResemblance.Like
集合助手[1],你
- 将它们按相同的顺序排列(这是在测试主体中最干净地完成的,因为输入和输出集合将是不同的类型,除非
SemanticComparison
增加一个特性,否则我认为它不能被有用地概括)
- 让
Ploeh.SemanticComparison
我们Likeness
在Name
- 让 xUnit.net V2 的 Assert.Collection 进行关联(它还没有给出更重要的信息)
最终结果是:
var results = from result in outputs orderby result.Name select result;
var expectations = from input in inputs orderby input.Name select input;
AssertResemblance.Like( expectations, results,
configure=>configure
.Without(x=>x.IgnoreThisField) );
[1]
static class AssertResemblance
{
public static void Like<T, T2>( IEnumerable<T> expected, IEnumerable<T2> actual )
{
Like<T, T2>( expected, actual, x=>x );
}
public static void Like<T, T2>( IEnumerable<T> expected, IEnumerable<T2> actual, Func<Likeness<T, T2>, Likeness<T, T2>> configureLikeness )
{
Assert2.Collection( actual, SelectShouldEqualAsAction( expected.Select( x => configureLikeness( x.AsSource().OfLikeness<T2>() ) ) ) );
}
public static void Like<T>( IEnumerable<T> expected, IEnumerable<T> actual, Func<IEnumerable<T>, IEnumerable<T>> unify )
{
Like<T>( expected, actual, unify, x=>x );
}
public static void Like<T>( IEnumerable<T> expected, IEnumerable<T> actual, Func<IEnumerable<T>,IEnumerable<T>> unify, Func<Likeness<T, T>, Likeness<T, T>> configureLikeness )
{
Assert2.Collection( unify( actual ), SelectShouldEqualAsAction( unify(expected).Select( x => configureLikeness( x.AsSource().OfLikeness<T>() ) ) ) );
}
static Action<TDestination>[] SelectShouldEqualAsAction<TSource, TDestination>( IEnumerable<Likeness<TSource, TDestination>> that )
{
return (from it in that select (Action<TDestination>)it.ShouldEqual).ToArray();
}
}