I have an object model that uses Open Generics (yes, yes, now I have two problems: thatβs why I am here :): -
public interface IOGF<T> { } class C { } class D { readonly IOGF<C> _ogf; public D( IOGF<C> ogf ) { _ogf = ogf; } }
I am trying to get AutoFixture to create anonymous D instances above. However, AutoFixture itself does not have a built-in strategy for building IOGF<> and, therefore, we observe:
public class OpenGenericsBinderDemo { [Fact] public void X() { var fixture = new Fixture(); Assert.Throws<Ploeh.AutoFixture.ObjectCreationException>( () => fixture.CreateAnonymous<D>() ); }
Main message:
Ploeh.AutoFixture.ObjectCreationException: AutoFixture could not create an instance from IOGF`1 [C], most likely because it does not have an open constructor, is abstract or non-public.
I am pleased to provide it with a specific implementation:
public class OGF<T> : IOGF<T> { public OGF( IX x ) { } } public interface IX { } public class X : IX { }
And related binding:
fixture.Register<IX,X>();
How can I (or should I even look at the problem this way?) To make the next test pass?
public class OpenGenericsLearning { [Fact] public void OpenGenericsDontGetResolved() { var fixture = new Fixture(); fixture.Inject<IX>( fixture.Freeze<X>() );
(There are discussions and problems on the Codeplex website - I just needed to quickly figure it out, and I'm open to removing this if it's just a bad idea and / or I missed something)
EDIT 2: (See also comment on Mark's answer.) The context (admittedly, far-fetched) is an acceptance test on a large "almost complete" system chart of the "System under test" object, and not on a small (controlled / easily accessible): pairs or triplets of classes in a single or integration test scenario. As mentioned in the summary statement about myself, I'm not entirely sure that this type of test even makes sense.
Ruben bartelink
source share