相关疑难解决方法(0)

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

我正在尝试克服一个类具有字符串构造函数参数的情况,该参数不能被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)

unit-testing autofixture

11
推荐指数
2
解决办法
4232
查看次数

标签 统计

autofixture ×1

unit-testing ×1