本文介绍了神似 - 打磨和包装的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的 Ploeh.SemanticComparison 神似,以此来有效地表达映射的预期产出过程(rel=\"nofollow\">上PluralSight )

I'm using Ploeh.SemanticComparison's Likeness as a way to effectively express intended outputs of a mapping process (as described in Mark Seemann's excellent Advanced Unit Testing course on PluralSight).

我测试的一些数据已经正确映射,它看起来像这样:

I'm testing some data has mapped correctly, which looks like this:

[Theory, AutoData]
static void ShouldYieldIdentifierUpdatedEvent( Vendor sut, string name, string version, Guid id )
{
    var result = sut.SyncProduct( name, version, id );

    var expected = new { ProductId = id, Name = name, Version = version };
    expected.AsSource().OfLikeness<NewMappingsEvent>()
        .Without( y => y.ProgrammaticIdentifier)
        .ShouldEqual(result);
}



不过,我不开心: -

However, I'm not happy:-


  1. 我想一个名字申请的相似性(即我的名字。没有(Y => y.ProgrammaticIdentifier)定制)

  2. 我已经失去了对称 Assert.Equal(预期,实际的,比较器)(但我肯定需要从错误信息 ShouldEqual

  1. I want to apply a name to the Resemblance (i.e. name my .Without( y => y.ProgrammaticIdentifier) customization)
  2. I've lost the symmetry with Assert.Equal( expected,actual, comparer) (but I definitely need the error message from ShouldEqual)

有没有表达这种清洁方法所表达的约束范围内?

Is there a cleaner way to express this within the expressed constraints?

推荐答案

如果你有所谓的断言助手类 AssertResemblance (如[4])和静态帮助像[1]的范围,你可以说它是:

If you had an Assertion helper class called AssertResemblance (like [4]), and a static helper like [1] in scope you could say it like:

var expected = new { ProductId = id, Name = name, Version = version };
AssertResemblance.Like( expected, result, WithoutProgrammaticIdentifier );



或者,如果你有一个像[2],你可以做一个扩展方法,如:

Or if you had an extension method like [2], you could do it like:

AssertResemblance.Like( expected,result,x=>x.WithoutProgrammaticIdentifier());



或者你可以有两全其美的(无噪音作为第一片段)尚未命名相似(通过具有在一个扩展方法的实际参数impl)通过在扩展方法方面实施的局部静态辅助([2]),如[3]

Or you could have the best of both worlds (no noise as in the first snippet) yet naming the Resemblance (by having the actual impl in an extension method) by implementing a local static helper in terms of the extension method ([2]) as in [3].

[1]

public static Likeness<T, NewMappingsEvent> WithoutProgrammaticIdentifier<T>( Likeness<T, NewMappingsEvent> that )
{
    return that.Without( x => x.ProgrammaticIdentifier );
}

[2]

static class NewMappingsEventResemblances
{
    public static Likeness<T, NewMappingsEvent> WithoutProgrammaticIdentifier<T>( this Likeness<T, NewMappingsEvent> that )
    {
        return that.Without( x => x.ProgrammaticIdentifier );
    }
}

[3]

static Likeness<T, NewMappingsEvent> WithoutProgrammaticIdentifier<T>( Likeness<T, NewMappingsEvent> that )
{
    return that.WithoutProgrammaticIdentifier();
}

[4]

static class AssertResemblance
{
    public static void Like<T, T2>( T expected, T2 actual )
    {
        Like( expected, actual, x => x );
    }

    public static void Like<T, T2>( T expected, T2 actual, Func<Likeness<T, T2>, Likeness<T, T2>> configureLikeness )
    {
        var likeness = expected.AsSource().OfLikeness<T2>();
        configureLikeness( likeness ).ShouldEqual( actual );
    }
}

这篇关于神似 - 打磨和包装的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 16:57