private static void Test(string serviceName, Action<IGenerateFactory<ISampleService>> usingFactory, Func<IUsingLambda<ISampleService>, IGenerateFactory<ISampleService>> doInject, Func<string, IServiceContainer, bool> verifyResult) { var container = new ServiceContainer(); // HACK: Manually inject the required services into the container container.AddDefaultServices(); Inject(serviceName, usingFactory, doInject, container); verifyResult(serviceName, container); }
public void ShouldResolveConstructorWithMostResolvableParametersFromContainer() { var mockSampleService = new Mock<ISampleService>(); IServiceContainer container = new ServiceContainer(); // Add an ISampleService instance container.AddService(mockSampleService.Object); container.AddDefaultServices(); var resolver = container.GetService<IMemberResolver<ConstructorInfo>>(); Assert.IsNotNull(resolver); // The resolver should return the constructor with two ISampleService parameters var expectedConstructor = typeof (SampleClassWithMultipleConstructors).GetConstructor(new[] { typeof (ISampleService), typeof (ISampleService) }); Assert.IsNotNull(expectedConstructor); var finderContext = new MethodFinderContext(new Type[0], new object[0], null); var result = resolver.ResolveFrom(typeof (SampleClassWithMultipleConstructors), container, finderContext); Assert.AreSame(expectedConstructor, result); }
public void ShouldResolveConstructorWithAdditionalArgument() { var mockSampleService = new Mock<ISampleService>(); IServiceContainer container = new ServiceContainer(); // Add an ISampleService instance container.AddService(mockSampleService.Object); container.AddDefaultServices(); var resolver = container.GetService<IMemberResolver<ConstructorInfo>>(); Assert.IsNotNull(resolver); // The resolver should return the constructor // with the following signature: Constructor(ISampleService, int) var expectedConstructor = typeof (SampleClassWithAdditionalArgument).GetConstructor(new[] {typeof (ISampleService), typeof (int)}); Assert.IsNotNull(expectedConstructor); var context = new MethodFinderContext(42); var result = resolver.ResolveFrom(typeof (SampleClassWithAdditionalArgument), container, context); Assert.AreSame(expectedConstructor, result); }
public void ShouldResolveClassWithMultipleNonServiceArgumentConstructors() { IServiceContainer container = new ServiceContainer(); container.AddDefaultServices(); container.AddService("ClassWithMultipleNonServiceArgumentConstructors", typeof (ISampleService), typeof (SampleClassWithMultipleNonServiceArgumentConstructors), LifecycleType.OncePerRequest); // Match the correct constructor var sampleService = container.GetService<ISampleService>("ClassWithMultipleNonServiceArgumentConstructors", "test", 42, SampleEnum.One, (decimal) 3.14, 42); Assert.IsNotNull(sampleService); }
public void ShouldInstantiateObjectWithConstructorAndArguments() { var targetConstructor = typeof (SampleClassWithMultipleConstructors).GetConstructor(new[] { typeof ( ISampleService ) , typeof ( ISampleService ) }); // Create the method arguments var mockSampleService = new Mock<ISampleService>(); var arguments = new object[] {mockSampleService.Object, mockSampleService.Object}; // Initialize the container IServiceContainer container = new ServiceContainer(); container.AddDefaultServices(); var constructorInvoke = container.GetService<IMethodInvoke<ConstructorInfo>>(); var result = constructorInvoke.Invoke(null, targetConstructor, arguments); Assert.IsNotNull(result); Assert.IsInstanceOfType(typeof (SampleClassWithMultipleConstructors), result); }
public void ShouldInstantiateClassWithNonServiceArguments() { var container = new ServiceContainer(); container.AddDefaultServices(); container.AddService(typeof (SampleClassWithNonServiceArgument), typeof (SampleClassWithNonServiceArgument)); var text = "Hello, World!"; string serviceName = null; var result = container.GetService<SampleClassWithNonServiceArgument>(serviceName, text); Assert.IsNotNull(result); Assert.IsTrue(result.Value == text); }