Class with a nested collection - how do I populate the nested class?

StackOverflow https://stackoverflow.com/questions/5458137

  •  12-11-2019
  •  | 
  •  

سؤال

I'm a bit confused as to hydrate a class with a nested collection of another class. I get the error:

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

I've tried to use Fixture.Register() to register the type for populating the inner class. I could just to do with a sample which shows

  1. The main class
  2. The nested collection (IList/IEnumerable) of another class.

I've also noticed that Register() is marked as obsolete, yet there is no documentation on the Inject() method on CodePlex so I'm stumped as to how this should work.

هل كانت مفيدة؟

المحلول

AutoFixture makes no presumptions on how to instantiate instances of interfaces, but you can pretty easily enable the behavior you seek - e.g. for integers:

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

In the upcoming AutoFixture 2.1 you can also just enable conventions for various collections with a single method call:

var fixture = new Fixture().Customize(new MultipleCustomization());

Regarding the deprecation of the Register method, please notice that it's only a single overload of the Register method that's marked obsolete. The Inject method is the direct replacement - it has the same signature and usage, just a different name. All the other Register overloads are not deprecated and should be used as 'documented'.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top