public void FactoryAttributeLoaderMustInjectUnnamedCustomFactoryIntoContainer() { var mockContainer = new Mock <IServiceContainer>(); var mockPreProcessors = new Mock <IList <IPreProcessor> >(); Type serviceType = typeof(ISampleService); string serviceName = null; // The container should add the expected // factory type mockContainer.Expect( container => container.AddFactory(serviceName, serviceType, It.IsAny <IEnumerable <Type> >(), It.IsAny <SampleFactory>())); // The factory attribute loader will add the custom // factory to the preprocessors collection mockContainer.Expect(container => container.PreProcessors) .Returns(mockPreProcessors.Object); mockPreProcessors.Expect(p => p.Add(It.IsAny <IPreProcessor>())); ITypeLoader loader = new FactoryAttributeLoader(); IEnumerable <Action <IServiceContainer> > actions = loader.Load(typeof(SampleFactory)); // The factory loader should return a set of actions // that will inject that custom factory into the container // itself foreach (var action in actions) { action(mockContainer.Object); } mockContainer.VerifyAll(); }
public void FactoryAttributeLoaderMustInjectOpenGenericServiceTypeIntoContainer() { var mockContainer = new Mock <IServiceContainer>(); Type serviceType = typeof(ISampleGenericService <>); var mockPostProcessors = new Mock <IList <IPostProcessor> >(); var mockPreProcessors = new Mock <IList <IPreProcessor> >(); ITypeLoader loader = new FactoryAttributeLoader(); IEnumerable <Action <IServiceContainer> > actions = loader.Load(typeof(SampleOpenGenericFactory)); // The loader should load the mock container // using the generic open type as the service type mockContainer.Expect(container => container.PostProcessors) .Returns(mockPostProcessors.Object); mockContainer.Expect(container => container.PreProcessors) .Returns(mockPreProcessors.Object); mockContainer.Expect(container => container.AddFactory(null, serviceType, It.IsAny <IEnumerable <Type> >(), It.IsAny <SampleOpenGenericFactory>())); // The postprocessor list should have an additional element added mockPostProcessors.Expect(p => p.Add(It.IsAny <IPostProcessor>())); // The preprocessor list should have an additional element added as well mockPreProcessors.Expect(p => p.Add(It.IsAny <IPreProcessor>())); Action <IServiceContainer> applyActions = container => { foreach (var action in actions) { action(container); } }; applyActions(mockContainer.Object); // Apply the actions to a real container and verify // it with the expected service instance var realContainer = new ServiceContainer(); applyActions(realContainer); }
public void FactoryAttributeLoaderMustInjectStronglyTypedFactoryIntoContainer() { var container = new ServiceContainer(); Type serviceType = typeof(ISampleService); ITypeLoader loader = new FactoryAttributeLoader(); IEnumerable <Action <IServiceContainer> > actions = loader.Load(typeof(SampleStronglyTypedFactory)); // The factory loader should return a set of actions // that will inject that custom factory into the container // itself foreach (var action in actions) { action(container); } Assert.IsTrue(container.Contains(serviceType)); }