Likeness - polishing and packaging

梦想与她 提交于 2019-12-02 04:15:00

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 );

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

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

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 );
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!