如何使用Autofixture(v3)与ICustomization,ISpecimenBuilder来处理构造函数参数?

Jef*_*eff 11 unit-testing autofixture

我正在尝试克服一个类具有字符串构造函数参数的情况,该参数不能被Autofixture生成的任何旧字符串(Guid-y外观值)所满足.

在您想要简单地回复Mark Seemann的关于基于会议的自定义的Ploeh博客文章的链接之前,请允许我说我一直在引用它和他的其他博客条目进行此测试,我无法访问通过.

当我在调试中单步执行时,我可以看到构造函数参数在某些时候传入了有效值,但测试仍然失败并带有Guid-y Color值.我认为这与"自动混合"填充"颜色"参数值 "颜色"属性这一事实有关.是不是我编写了一个解决构造函数参数的ISpecimenBuilder,但我正在测试公共属性值(两个不同的东西)?

我知道所有这些对于该示例来说都是过度的,但我想到了一个更复杂的场景,其中使用该Build<T>().With()方法将不会是DRY.

失败测试

    [Fact]
    public void Leaf_Color_Is_Brown()
    {
        // arrange
        var fixture = new Fixture().Customize(new LeafColorCustomization());

        // act
        var leaf = fixture.Create<Leaf>();

        // using .Build<>.With(), test passes
        //var leaf = fixture.Build<Leaf>().With(l => l.Color, "brown").CreateAnonymous();

        // assert
        Assert.True(leaf.Color == "brown");
    }
Run Code Online (Sandbox Code Playgroud)

SUT

    public class Leaf
    {
        public Leaf(string color)
        {
            if (color != "brown")
                throw new ArgumentException(@"NO LEAF FOR YOU!");

            this.Color = color;
        }
        public string Color { get; set; }
    }
Run Code Online (Sandbox Code Playgroud)

CompositeCustomization实现(我知道在这个例子中不需要AutoMoqCustomization())

    public class LeafCustomization : CompositeCustomization
    {
        public LeafCustomization()
            : base(
            new LeafColorCustomization(),
            new AutoMoqCustomization()) { }
    }
Run Code Online (Sandbox Code Playgroud)

叶特定的ICustomization

    public class LeafColorCustomization : ICustomization
    {
        public void Customize(IFixture fixture)
        {
            if (fixture == null)
                throw new ArgumentNullException("fixture");

            fixture.Customizations.Add(new LeafBuilder());
        }
    }
Run Code Online (Sandbox Code Playgroud)

String-constructor-with-name-of-Color特定的ISpecimenBuilder

    public class LeafBuilder : ISpecimenBuilder
    {
        public object Create(object request, ISpecimenContext context)
        {
            var pi = request as ParameterInfo;
            if (pi == null)
                return new NoSpecimen(request);

            if (pi.ParameterType != typeof(string) || pi.Name != "color")
                return new NoSpecimen(request);

            return "brown";
        }
    }
Run Code Online (Sandbox Code Playgroud)

Nik*_*nis 7

解决方案1:

注册Color不应为可写属性分配任何自动值作为后处理的一部分:

internal class LeafColorCustomization : ICustomization
{
    public void Customize(IFixture fixture)
    {
        fixture.Customize<Leaf>(c => c
            .Without(x => x.Color));

        fixture.Customizations.Add(new LeafBuilder());
    }
}
Run Code Online (Sandbox Code Playgroud)

解决方案2:

使Color属性为只读:

public class Leaf
{
    private readonly string color;

    public Leaf(string color)
    {
        if (color != "brown")
            throw new ArgumentException(@"NO LEAF FOR YOU!");

        this.color = color;
    }

    public string Color
    {
        get { return this.color; }
    }
}
Run Code Online (Sandbox Code Playgroud)

由于Color属性是只读的,因此AutoFixture不会为其分配值.

以上解决方案也适用于AutoFixture 2.

  • @Lumirris你绝对可以在`LeafBuilder`中处理这个问题.除了你已经处理过`ParameterInfo`的情况之外,你还需要处理`request`是`PropertyInfo`的情况. (3认同)
  • 在这种特殊情况下,我肯定会使用像`LeafBuilder`这样的`ISpecimenBuilder`,因为它可以让你实现细粒度,基于约定的启发式方法.OTOH,它比使用`IFixture`暴露的强类型方法更有用,所以如果我能以这种方式表达我想要的东西,我会使用它们. (3认同)

Rub*_*ink 5

假设你单独处理属性设置的东西,这里是一个构造函数参数限制Customization,它可以解决这个问题:

class BrownLeavesCustomization : ICustomization
{
    void ICustomization.Customize( IFixture fixture )
    {
        Func<string> notBrownGenerator = fixture.Create<Generator<string>>()
            .SkipWhile( x => x == "Brown" )
            .First;
        fixture.Customizations.Add( 
            ArgumentGeneratorCustomization<Leaf>.ForConstructorArgument(
                "color", 
                notBrownGenerator ) );
    }

    static class ArgumentGeneratorCustomization<T>
    {
        public static ISpecimenBuilder ForConstructorArgument<TArg>( string argumentName, Func<TArg> generator )
        {
            return new ConstructorArgumentGenerator<TArg>( argumentName, generator );
        }

        class ConstructorArgumentGenerator<TArg> : ISpecimenBuilder
        {
            readonly string _argumentName;
            readonly Func<TArg> _generator;

            public ConstructorArgumentGenerator( string argumentName, Func<TArg> generator )
            {
                Assert.Contains( argumentName, from ctor in typeof( T ).GetConstructors() from param in ctor.GetParameters() select param.Name );
                _argumentName = argumentName;
                _generator = generator;
            }

            object ISpecimenBuilder.Create( object request, ISpecimenContext context )
            {
                var pi = request as ParameterInfo;
                if ( pi == null )
                    return new NoSpecimen( request );
                if ( pi.Member.DeclaringType != typeof( T ) )
                    return new NoSpecimen( request );
                if ( pi.Member.MemberType != MemberTypes.Constructor )
                    return new NoSpecimen( request );
                if ( pi.ParameterType != typeof( TArg ) )
                    return new NoSpecimen( request );
                if ( pi.Name != _argumentName )
                    return new NoSpecimen( request );

                return _generator();
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)