autofixture - 带有嵌套集合的类 - 如何填充嵌套类?

标签 autofixture

对于将一个类与另一个类的嵌套集合进行水合,我有点困惑。
我得到错误:

AutoFixture was unable to create an instance from System.Collections.Generic.IList`1[typename...]



我尝试使用 Fixture.Register()注册用于填充内部类的类型。
我可以只做一个显示的样本
  • 主类
  • 另一个类的嵌套集合 (IList/IEnumerable)。

  • 我还注意到 Register()被标记为过时,但 Inject() 上没有文档CodePlex 上的方法,所以我很难理解它应该如何工作。

    最佳答案

    AutoFixture 不假设如何实例化接口(interface)实例,但您可以 pretty easily enable the behavior you seek - 例如对于整数:

    fixture.Register(() => fixture.CreateMany<int>());
    

    在即将推出的 AutoFixture 2.1 中,您也可以只使用 enable conventions for various collections使用单个方法调用:
    var fixture = new Fixture().Customize(new MultipleCustomization());
    

    关于 Register 方法的弃用,请注意它只是 Register 方法的一个重载被标记为过时。 Inject 方法是直接替换 - 它具有相同的签名和用法,只是名称不同。所有其他寄存器重载均未弃用,应作为“记录”使用。

    关于autofixture - 带有嵌套集合的类 - 如何填充嵌套类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5458137/

    相关文章:

    c# - 使用 FromSeed 自定义 AutoFixure 导致异常

    c# - AutoFixture+Moq - 卡住模拟类以防止设置

    moq - 什么是 AutoFixture AutoMoq?

    c# - 如何在 Moq 中使用 lambda 语法设置现有模拟?

    .net - 如何发出具有两个仅在返回类型上重载的属性的 .NET 类型?

    c# - Autofixture Fixture.Customize - 为什么重载有不同的返回类型?

    c# - AutoFixture AutoMoq 将模拟对象转换为接口(interface)

    c# - 为什么 AutoFixture.AutoMoq 默认进行递归模拟?

    c# - 如何验证调用了一个方法?

    c# - 指定单个构造函数参数值的简单方法?