简体   繁体   English

IList的 <something> 构造函数参数和AutoFixture

[英]IList<something> constructor parameter and AutoFixture

Using autofixture , I'm trying to construct anonymous instance of Project : 使用自动混合 ,我正在尝试构建Project匿名实例:

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

This fails, cause Project public constructor demands IList<Partner> 这会失败,导致Project公共构造函数需要IList<Partner>

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

Stack trace isn't meaningful (at least - for me). 堆栈跟踪没有意义(至少对我而言)。 Just some reflection yada-yada: 只是一些反思yada-yada:

failed: System.Reflection.TargetInvocationException : Exception has been thrown by the target of an invocation. failed:System.Reflection.TargetInvocationException:调用目标抛出了异常。
---- System.ArgumentException : Value does not fall within the expected range. ---- System.ArgumentException:值不在预期范围内。
at System.RuntimeMethodHandle._InvokeConstructor(IRuntimeMethodInfo method, Object[] args, SignatureStruct& signature, RuntimeType declaringType) 在System.RuntimeMethodHandle._InvokeConstructor(IRuntimeMethodInfo方法,Object [] args,SignatureStruct&signature,RuntimeType declaringType)

So - how to make sure autoFixture passses in anonymous collection of partners in order to construct it? 那么 - 如何确保autoFixture在匿名合作伙伴集合中传递以构建它?


It's not fault of IList<Partners> . 这不是IList<Partners>错。 There's another parameter called Priority . 还有一个名为Priority的参数。 Priority itself holds Measure , Measure holds IList<Indicator> and calls Guard.AgainstEmpty(indicators) in constructor. Priority本身持有MeasureMeasure持有IList<Indicator>并在构造函数中调用Guard.AgainstEmpty(indicators)

So it looks something like this: 所以它看起来像这样:

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{}

Construction fails in Guard.AgainstEmpty method. Guard.AgainstEmpty方法构造失败。 So - the question becomes - how to make sure AutoFixture fills some bars in bars collection before constructing foos? 所以 - 问题变成了 - 如何确保AutoFixture在构建foos之前填充了bar集合中的一些条形图?

This helps. 这有帮助。 Browsing source often helps. 浏览通常有帮助。

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

There might be better way though. 可能有更好的方法。


In overall, this is how it looks at the moment: 总的来说,这就是当下的表现:

  _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>();

Can't call that beautiful (any refactoring suggestions are welcome), but it's still much better than writing everything manually. 不能称之为漂亮(欢迎任何重构建议),但它仍然比手动编写所有内容要好得多。


Using IList there is a wrong choice for sure. 使用IList肯定是错误的选择。 Even worse - I'm using IList for properties too. 更糟糕的是 - 我也在使用IList作为属性。 That invites client to use them directly instead of going through aggregate root. 这会邀请客户直接使用它们而不是通过聚合根。

There is one drawback when using params . 使用params时有一个缺点。 Can't use more than one (unless I'm missing some basics again). 不能使用多个(除非我再次遗漏一些基础知识)。 And I'm receiving list as an input (part of excel sheet DOM), Can't know compile time how much elements will be there. 而且我收到列表作为输入(excel表DOM的一部分),无法知道编译时间将有多少元素。

Model is really fresh. 模特真的很新鲜。 Just baked it (so there is great chance that I'm wrong about those emptiness checks, will talk with client and business analyst about that). 刚刚烘焙它(因此很有可能我对这些空白检查错了,会与客户和业务分析师讨论这个问题)。

My strategy is to freely sculpture it and push it towards desired state with unit tests. 我的策略是通过单元测试自由地雕刻它并将其推向所需的状态。 This is actual reason I dislike strict TDD a bit. 这是我不喜欢严格的TDD的实际原因。 It steals focus, forces me to think about details instead of whole picture kinda too soon. 它偷走了焦点,迫使我过早地考虑细节而不是整个画面。 I prefer to sketch it and refine until it looks good. 我更喜欢草绘它并精炼直到它看起来很好。 But that might be cause I'm not fluent enough with testing. 但这可能是因为我对测试不够流利。

Anyway - thank You for great tips. 无论如何 - 谢谢你的好建议。 I will continue to learn more about AutoFixture. 我将继续了解有关AutoFixture的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM