示例#1
0
        public void CreateService_Type_PopulatesConstructorParams()
        {
            // Arrange

            var logger = new Mock <ILogger>().Object;

            var serviceProvider = new Mock <IServiceProvider>();

            serviceProvider
            .Setup(m => m.GetService(typeof(ILogger)))
            .Returns(logger);

            var factory = new SingletonServiceFactory(typeof(WithParametersType));

            factory.Initialize(serviceProvider.Object);

            // Act

            var result = factory.CreateService(typeof(WithParametersType));

            // Assert

            Assert.NotNull(result);
            var typedResult = Assert.IsAssignableFrom <WithParametersType>(result);

            Assert.Equal(logger, typedResult.Logger);
        }
示例#2
0
        public void CreateService_LambdaCalledXTimes_ReturnsSingleton(int times)
        {
            // Arrange

            var serviceProvider = new Mock <IServiceProvider>();

            var factory = new SingletonServiceFactory(localServiceProvider =>
            {
                Assert.Equal(serviceProvider.Object, localServiceProvider);

                return(new object());
            });

            factory.Initialize(serviceProvider.Object);

            // Act

            var result = Enumerable.Range(1, times).Select(p => factory.CreateService(typeof(object))).ToList();

            // Assert

            var obj = result.First();

            Assert.All(result, p => Assert.Equal(obj, p));
        }
示例#3
0
        public void CreateService_CalledXTimes_ReturnsSingleton(int times)
        {
            // Arrange

            var obj = new object();

            var factory = new SingletonServiceFactory(obj);

            // Act

            var result = Enumerable.Range(1, times).Select(p => factory.CreateService(typeof(object)));

            // Assert

            Assert.All(result, p => Assert.Equal(obj, p));
        }
示例#4
0
        public void CreateService_Type_ReturnsSingleton(int times)
        {
            // Arrange

            var serviceProvider = new Mock <IServiceProvider>();

            var factory = new SingletonServiceFactory(typeof(NoParametersType));

            factory.Initialize(serviceProvider.Object);

            // Act

            var result = Enumerable.Range(1, times).Select(p => factory.CreateService(typeof(NoParametersType))).ToList();

            // Assert

            var obj = result.First();

            Assert.All(result, p => Assert.Equal(obj, p));
        }