public void Can_specify_to_use_default_ctor() { var container = new Container(); container.Register <Ab>(made: FactoryMethod.DefaultConstructor()); var ab = container.Resolve <Ab>(); Assert.IsNotNull(ab); }
public void Can_specify_to_use_default_ctor_and_throw_correct_error_if_no_impl_type() { var container = new Container(); container.Register <BaseA>(made: FactoryMethod.DefaultConstructor()); var ex = Assert.Throws <ContainerException>(() => container.Resolve <BaseA>()); Assert.AreEqual(Error.NameOf(Error.ImplTypeIsNotSpecifiedForAutoCtorSelection), ex.ErrorName); }
public void TestCase_with_dynamic_registration() { var testDynamicResolutionAttempts = 0; var prodContainer = new Container(); using (var container = prodContainer.CreateChild(IfAlreadyRegistered.Replace, prodContainer.Rules.WithDynamicRegistration( (serviceType, serviceKey) => { ++testDynamicResolutionAttempts; if (serviceType.IsInterface && serviceType.IsOpenGeneric() == false) { var mockType = typeof(Mock <>).MakeGenericType(serviceType); return(new[] { new DynamicRegistration( new DelegateFactory(r => ((Mock)r.Resolve(mockType)).Object, Reuse.Singleton, Setup.Default, serviceType), IfAlreadyRegistered.Keep), }); } return(null); }, DynamicRegistrationFlags.Service | DynamicRegistrationFlags.AsFallback) )) { container.Register(typeof(Mock <>), Reuse.Singleton, made: FactoryMethod.DefaultConstructor()); container.Register <UnitOfWork>(Reuse.Singleton); // Arrangements const bool expected = true; container.Resolve <Mock <IDep> >() .Setup(instance => instance.Method()) .Returns(expected); // Get concrete type instance of tested unit // all dependencies are fulfilled with mocked instances var unit = container.Resolve <UnitOfWork>(); // Action var actual = unit.InvokeDep(); // Assertion Assert.AreEqual(expected, actual); container.Resolve <Mock <IDep> >() .Verify(instance => instance.Method()); Assert.AreEqual(1, testDynamicResolutionAttempts); } }
public void Can_specify_to_use_default_ctor_and_throw_correct_error_if_no_default_ctor() { var container = new Container(); container.Register <Ac>(made: FactoryMethod.DefaultConstructor()); var ex = Assert.Throws <ContainerException>(() => container.Resolve <Ac>()); Assert.AreEqual( Error.NameOf(Error.UnableToSelectCtor), Error.NameOf(ex.Error)); }
public static IContainer CreateTestContainer(IContainer container) { var c = container.CreateChild(IfAlreadyRegistered.Replace, container.Rules.WithDynamicRegistration((serviceType, serviceKey) => { // ignore services with non-default key if (serviceKey != null) { return(null); } if (serviceType == typeof(object)) { return(null); } // get the Mock object for the abstract class or interface if (serviceType.IsInterface || serviceType.IsAbstract) { // except for the open-generic ones if (serviceType.IsGenericType && serviceType.IsOpenGeneric()) { return(null); } var mockType = typeof(Mock <>).MakeGenericType(serviceType); var mockFactory = new DelegateFactory(r => ((Mock)r.Resolve(mockType)).Object, Reuse.Singleton); return(new[] { new DynamicRegistration(mockFactory, IfAlreadyRegistered.Keep) }); } // concrete types var concreteTypeFactory = new ReflectionFactory(serviceType, Reuse.Singleton, FactoryMethod.ConstructorWithResolvableArgumentsIncludingNonPublic); return(new[] { new DynamicRegistration(concreteTypeFactory) }); }, DynamicRegistrationFlags.Service | DynamicRegistrationFlags.AsFallback)); c.Register(typeof(Mock <>), Reuse.Singleton, FactoryMethod.DefaultConstructor()); return(c); }
public TestContainer(IContainer container = null) { _container = (container ?? new Container()).With(rules => rules.WithDynamicRegistration( (serviceType, serviceKey) => { ++MockAttempts; if (serviceType.IsInterface && serviceType.IsOpenGeneric() == false) { var mockType = typeof(Mock <>).MakeGenericType(serviceType); return(new[] { new DynamicRegistration( new DelegateFactory(r => ((Mock)r.Resolve(mockType)).Object, Reuse.Singleton, null, serviceType), IfAlreadyRegistered.Keep), }); } return(null); }, DynamicRegistrationFlags.Service | DynamicRegistrationFlags.AsFallback )); _container.Register(typeof(Mock <>), Reuse.Singleton, made: FactoryMethod.DefaultConstructor()); }