IList的<&东西GT;构造函数的参数和AutoFixture [英] IList<something> constructor parameter and AutoFixture

查看:193
本文介绍了IList的<&东西GT;构造函数的参数和AutoFixture的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 autofixture ,我试图构建项目

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

这失败,原因项目公共构造要求的IList<合伙GT;

This fails, cause Project public constructor demands IList<Partner>

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



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

Stack trace isn't meaningful (at least - for me). Just some reflection yada-yada:

失败:System.Reflection.TargetInvocationException:异常已被调用的目标引发异常

---- System.ArgumentException:值没有在预期范围内,结果
在System.RuntimeMethodHandle._InvokeConstructor(IRuntimeMethodInfo方法,对象[] ARGS,SignatureStruct&安培;签名,RuntimeType declaringType )

failed: System.Reflection.TargetInvocationException : Exception has been thrown by the target of an invocation.
---- System.ArgumentException : Value does not fall within the expected range.
at System.RuntimeMethodHandle._InvokeConstructor(IRuntimeMethodInfo method, Object[] args, SignatureStruct& signature, RuntimeType declaringType)

所以 - ?如何确保autoFixture passses在合作伙伴的匿名收集,以构建它

So - how to make sure autoFixture passses in anonymous collection of partners in order to construct it?

这不是故障的IList<的;合作伙伴> 。有一个名为另一个参数优先优先级本身持有测量测量持有的IList<指标方式> ,并在构造函数调用 Guard.AgainstEmpty(指标)

It's not fault of IList<Partners>. There's another parameter called Priority. Priority itself holds Measure, Measure holds IList<Indicator> and calls Guard.AgainstEmpty(indicators) in constructor.

因此,它看起来是这样的:

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

建设 Guard.AgainstEmpty 方法失败。所以 - 问题就变成了 - 如何确保AutoFixture建设FOOS前填写栏收集一些酒吧

Construction fails in Guard.AgainstEmpty method. So - the question becomes - how to make sure AutoFixture fills some bars in bars collection before constructing foos?

推荐答案

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

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.

使用的IList 有一个错误的选择当然。更糟糕的是 - 我用的IList 的性质了。 ,邀请客户使用它们,而不是直接通过聚合根去。

Using IList there is a wrong choice for sure. Even worse - I'm using IList for properties too. That invites client to use them directly instead of going through aggregate root.

使用时有一个缺点 PARAMS 。不能使用一个以上的(除非我再次失踪的一些基础知识)。而我收到清单作为输入(Excel工作表DOM的一部分),可不知编译的时候多元素怎么会在那里。

There is one drawback when using 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.

示范真是新鲜。刚刚出炉的它(所以有很大的机会,我错了,那些空虚的检查,将与有关客户和业务分析师交谈)。

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).

我的策略是自由的雕塑,并与单元测试将其推向理想状态。这是实际的原因,我不喜欢严格的TDD一点。它偷走焦点,迫使我有点儿太早考虑的细节,而不是全貌。我喜欢素描它和提炼,直到它看起来不错。不过,可能是因为我不是与测试来说已经足够流畅

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. 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.

反正 - 感谢你伟大的秘诀。我会继续了解AutoFixture。

Anyway - thank You for great tips. I will continue to learn more about AutoFixture.

这篇关于IList的&LT;&东西GT;构造函数的参数和AutoFixture的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆