Example #1
0
        public void ShouldAutoInjectClassCreatedWithAutoCreate()
        {
            // Configure the container
            var container = new ServiceContainer();
            container.LoadFromBaseDirectory("*.dll");

            var sampleService = new Mock<ISampleService>();
            container.AddService(sampleService.Object);

            var instance = (SampleClassWithInjectionProperties)container.AutoCreate(typeof(SampleClassWithInjectionProperties));

            // The container should initialize the SomeProperty method to match the mock ISampleService instance
            Assert.IsNotNull(instance.SomeProperty);
            Assert.AreSame(instance.SomeProperty, sampleService.Object);
        }
        public void ContainerMustInjectFactoryInstances()
        {
            var mockFactory = new Mock<IFactory<ISampleService>>();
            mockFactory.Expect(f => f.CreateInstance(It.IsAny<IFactoryRequest>())).Returns(new SampleClass());

            var container = new ServiceContainer();
            container.AddFactory(mockFactory.Object);

            var instance =
                (SampleClassWithFactoryDependency) container.AutoCreate(typeof (SampleClassWithFactoryDependency));

            Assert.IsNotNull(instance);

            IFactory<ISampleService> factory = instance.Factory;
            factory.CreateInstance(null);

            mockFactory.VerifyAll();
        }
Example #3
0
        public void ShouldInjectConstructorWithNamedParameterTypes()
        {
            var mockDefaultSampleService = new Mock<ISampleService>();
            var mockOtherSampleService = new Mock<ISampleService>();
            var container = new ServiceContainer();

            // Add the default service
            container.AddService(mockDefaultSampleService.Object);

            // Add the expected service instance
            container.AddService("OtherService", mockOtherSampleService.Object);

            var serviceInstance =
                (SampleClassWithNamedParameters) container.AutoCreate(typeof (SampleClassWithNamedParameters));

            Assert.AreEqual(mockOtherSampleService.Object, serviceInstance.ServiceInstance);
        }
Example #4
0
        public void ShouldCreateTypeWithAdditionalParameters()
        {
            var mockSampleService = new Mock<ISampleService>();
            IServiceContainer container = new ServiceContainer();

            // Add an ISampleService instance
            container.AddService(mockSampleService.Object);

            var resolver = container.GetService<IMemberResolver<ConstructorInfo>>();
            Assert.IsNotNull(resolver);

            var instance =
                container.AutoCreate(typeof (SampleClassWithAdditionalArgument), 42) as
                    SampleClassWithAdditionalArgument;
            Assert.IsNotNull(instance);
            Assert.IsTrue(instance.Argument == 42);
        }