Using autofixture, I'm trying to construct anonymous instance of Project
:
_f=new Fixture().Customize(new AutoMoqCustomization());
_p=_f.CreateAnonymous<Project>();
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:
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)
So - how to make sure autoFixture passses in anonymous collection of partners in order to construct it?
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{}
Construction fails in Guard.AgainstEmpty
method. So - the question becomes - how to make sure AutoFixture fills some bars in bars collection before constructing foos?