代码之家  ›  专栏  ›  技术社区  ›  Arnis Lapsa

ilist<something>构造函数参数和autofixture

  •  3
  • Arnis Lapsa  · 技术社区  · 15 年前

    使用 autofixture ,我正在尝试构造的匿名实例 Project :

     _f=new Fixture().Customize(new AutoMoqCustomization());
     _p=_f.CreateAnonymous<Project>();
    

    这失败了,因为 项目 公共建设者需求 IList<Partner>

    public Project(/*.....*/,IList<Partner> partners){
      Guard.AgainstEmpty(partners);
    }
    

    堆栈跟踪没有意义(至少对我来说)。只是一些反思雅达雅达:

    失败:System.Reflection.TargetInvocationException:调用的目标引发了异常。
    ----System.ArgumentException:值不在预期范围内。
    at System.RuntimeMethodHandle._InvokeConstructor(IruntimeMethodInfo方法,Object[]参数,SignatureStruct&签名,RuntimeType声明类型)

    那么-如何确保匿名合作伙伴集合中的autofixture passes来构造它?


    这不是…的错 IList<Partners> . 还有一个参数叫做 Priority . 优先 本身持有 Measure , 措施 持有 IList<Indicator> 和电话 Guard.AgainstEmpty(indicators) 在构造函数中。

    所以看起来是这样的:

    fixture.CreateAnonymous<Foo>(); //kaboom!
    public class Foo{
      public Foo(IList<Bar> bars){
        Guard.AgainstEmpty(bars); //just checks count for ienumerable & throws if 0
        Bars=bars;
      }
      public IList<Bar> Bars {get;private set;} //should be readonly collection...
    }
    
    public class Fizz{
      public Fizz(Foo foo){
        Foo=foo;
      }
      public Foo{get;private set;}
    }
    
    public class Bar{}
    

    施工失败 Guard.AgainstEmpty 方法。所以,问题变成了,在构建foos之前,如何确保autofixture填充了bars集合中的一些条?

    1 回复  |  直到 15 年前
        1
  •  1
  •   Arnis Lapsa    15 年前

    这有帮助。浏览 source 经常有帮助。

    var indicators=_f.CreateMany<Indicator>();
    _f.Register<IList<Indicator>>(()=>indicators.ToList());
    

    不过,也许还有更好的办法。


    总的来说,这就是目前的情况:

      _f=new Fixture().Customize(new AutoMoqCustomization());
      var indicators=_f.CreateMany<Indicator>();
      _f.Register<IList<Indicator>>(()=>indicators.ToList());
      var regionName=_f.CreateAnonymous<string>();
      _f.Register<string,Country,bool,Region>((name,country,call)=>
        new Region(regionName,_f.CreateAnonymous<Country>(),true));
      _c.Set(x=>x.Regions,_f.CreateMany<Region>().ToList());
      _f.Register<IList<ManagementBoardEntry>>(()=>
        _f.CreateMany<ManagementBoardEntry>().ToList());
      _f.Register<IList<FinancialInfoEntry>>(()=>
        _f.CreateMany<FinancialInfoEntry>().ToList());
      _f.Register<IList<Partner>>(()=>_f.CreateMany<Partner>().ToList());
      _p=_f.CreateAnonymous<Project>();
    

    不能称之为漂亮(任何重构建议都是受欢迎的),但它仍然比手动编写所有东西要好得多。


    使用 IList 当然有一个错误的选择。更糟的是-我在用 伊利斯特 也适用于属性。这会邀请客户机直接使用它们,而不是遍历聚合根目录。

    使用时有一个缺点 params . 不能使用多个(除非我又缺少一些基础知识)。我接收列表作为输入(ExcelSheetDOM的一部分),不知道编译时会有多少元素。

    模特真的很新鲜。刚刚烤好了(所以很有可能我对那些空虚的检查是错的,我会和客户和业务分析师讨论这个问题)。

    我的策略是自由地雕刻它,并通过单元测试将其推向所需的状态。这就是我不喜欢严格的TDD的真正原因。它偷走了焦点,迫使我思考细节而不是整个画面有点太早。我更喜欢把它画出来并加以改进,直到它看起来好为止。但这可能是因为我对测试不够流利。

    不管怎样-谢谢你的好建议。我将继续学习有关自动固定的更多信息。

    推荐文章