【问题标题】:Autofixture Fixture.Build().With() On Same Property NameAutofixture Fixture.Build().With() 在相​​同的属性名称上
【发布时间】:2013-05-06 07:15:50
【问题描述】:

当我使用“with”方法设置属性时,它会将同名嵌套对象上的所有属性都保留为空。

(我使用autofixture的最新版本为3.0.8)

public class Something {
    public string Id { get; set; }
    public IList<Something> Things { get; set; }
}

var obj = Fixture.Build<Something>().With(q => q.Id, "something").CreateAnonymous()

在这种情况下,obj.Id == "something" 等于 true,但 obj.Things[0].Id == null 也等于 true。

我认为有错误或我弄错了;有人可以帮忙吗?

【问题讨论】:

    标签: c# unit-testing autofixture


    【解决方案1】:

    默认情况下,AutoFixture 不会创建 Something 的实例,因为该图包含循环引用。

    您可以做的是在Fixture 实例上添加/删除适当的行为:

    fixture.Behaviors.Remove(new ThrowingRecursionBehavior());
    fixture.Behaviors.Add(new OmitOnRecursionBehavior());
    

    您现在可以创建Something 的实例,但是现在省略了Things 属性(循环引用)。

    这就是为什么你得到一个空列表..

    但是,您可以进一步自定义创建算法:

    var obj = fixture.Build<Something>()
        .With(x => x.Id, 
            "something")
        .With(x => x.Things, 
            fixture.CreateMany<Something>().ToList())
        .Create();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-12
      • 1970-01-01
      相关资源
      最近更新 更多