public void ResolveAll_NoRegisteration_ReturnsEmptyEnumerable()
        {
            var subject = new ServiceLocatorContainer();
            var result  = subject.ResolveAll <IDisposable>();

            Assert.IsEmpty(result);
        }
예제 #2
0
        private static IServiceLocator CreateServiceLocator(Configuration configuration)
        {
            var container = new ServiceLocatorContainer(configuration.ServiceLocatorSetup);

            container.RegisterSingleton <Configuration, Configuration>(configuration);

            return(new ServiceLocator(container));
        }
예제 #3
0
        public void Resolve_NonRegisteredTypeWithDefaultConstructor_ThrowsException()
        {
            var subject = new ServiceLocatorContainer();

            Assert.Throws <InvalidOperationException>(() =>
            {
                subject.Resolve <MemoryStream>();
            });
        }
예제 #4
0
        public void Resolve_NonRegisteredInterface_ThrowsException()
        {
            var subject = new ServiceLocatorContainer();

            Assert.Throws <InvalidOperationException>(() =>
            {
                subject.Resolve <IDisposable>();
            });
        }
예제 #5
0
        public void Resolve_NonRegisteredTypeWithDefaultConstructor_ThrowsException()
        {
            var subject = new ServiceLocatorContainer();

            Assert.Throws<InvalidOperationException>(() =>
            {
                subject.Resolve<MemoryStream>();
            });
        }
예제 #6
0
        public void Resolve_NonRegisteredInterface_ThrowsException()
        {
            var subject = new ServiceLocatorContainer();

            Assert.Throws<InvalidOperationException>(() =>
            {
                subject.Resolve<IDisposable>();
            });
        }
예제 #7
0
        public void Bootstrap()
        {
            // Register dependencies
            var builder = new ContainerBuilder();

            new ApiBootstrapper().Bootstrap(builder, "N/A", "UnitTests");
            var container = builder.Build();

            ServiceLocatorContainer.SetCurrent(new ServiceLocator(container));
        }
        public void Resolve_MultipleRegisterations_ReturnsLastRegistration()
        {
            var subject = new ServiceLocatorContainer();

            subject.Register <IDisposable, MemoryStream>();
            subject.Register <IDisposable, StringWriter>();

            IDisposable result = subject.Resolve <IDisposable>();

            Assert.IsTrue(result is StringWriter);
        }
예제 #9
0
        public void Resolve_RegisteredSingletonType_ReturnsSameInstancePerResolve()
        {
            var subject = new ServiceLocatorContainer();

            subject.RegisterSingleton <IDisposable, MemoryStream>();

            var first  = subject.Resolve <IDisposable>() as MemoryStream;
            var second = subject.Resolve <IDisposable>() as MemoryStream;

            Assert.NotNull(first);
            Assert.NotNull(second);
            Assert.AreSame(first, second);
        }
예제 #10
0
        public void Resolve_RegisteredType_CreatesNewInstancePerResolve()
        {
            var subject = new ServiceLocatorContainer();

            subject.Register <IDisposable, MemoryStream>();

            var first  = subject.Resolve <IDisposable>() as MemoryStream;
            var second = subject.Resolve <IDisposable>() as MemoryStream;

            Assert.NotNull(first);
            Assert.NotNull(second);
            Assert.AreNotSame(first, second);
        }
        public void ResolveAll_MultipleRegisterations_ReturnsInstanceOfEveryRegistration()
        {
            var subject = new ServiceLocatorContainer();

            subject.Register <IDisposable, MemoryStream>();
            subject.Register <IDisposable, StringWriter>();

            var result = subject.ResolveAll <IDisposable>()?.ToList();

            Assert.NotNull(result);
            Assert.AreEqual(2, result.Count);
            Assert.IsTrue(result.Any(x => x is StringWriter));
            Assert.IsTrue(result.Any(x => x is MemoryStream));
        }
예제 #12
0
        public void Constructor_ValidSetup_InvokesSetup()
        {
            var setupMock = new Mock<IServiceLocatorSetup>(MockBehavior.Strict);

            IServiceLocatorContainer calledContainer = null;
            setupMock
                .Setup(setup => setup.Configure(It.IsAny<IServiceLocatorContainer>()))
                .Callback<IServiceLocatorContainer>(container => calledContainer = container)
                .Verifiable();

            // Act
            var subject = new ServiceLocatorContainer(setupMock.Object);

            // Assert
            setupMock.Verify();
            Assert.AreEqual(subject, calledContainer);
        }
예제 #13
0
        public void Constructor_ValidSetup_InvokesSetup()
        {
            var setupMock = new Mock <IServiceLocatorSetup>(MockBehavior.Strict);

            IServiceLocatorContainer calledContainer = null;

            setupMock
            .Setup(setup => setup.Configure(It.IsAny <IServiceLocatorContainer>()))
            .Callback <IServiceLocatorContainer>(container => calledContainer = container)
            .Verifiable();

            // Act
            var subject = new ServiceLocatorContainer(setupMock.Object);

            // Assert
            setupMock.Verify();
            Assert.AreEqual(subject, calledContainer);
        }
예제 #14
0
        public void Resolve_RegisteredSingletonType_ReturnsSameInstancePerResolve()
        {
            var subject = new ServiceLocatorContainer();
            subject.RegisterSingleton<IDisposable, MemoryStream>();

            var first = subject.Resolve<IDisposable>() as MemoryStream;
            var second = subject.Resolve<IDisposable>() as MemoryStream;

            Assert.NotNull(first);
            Assert.NotNull(second);
            Assert.AreSame(first, second);
        }
예제 #15
0
        public void Resolve_RegisteredType_CreatesNewInstancePerResolve()
        {
            var subject = new ServiceLocatorContainer();
            subject.Register<IDisposable, MemoryStream>();

            var first = subject.Resolve<IDisposable>() as MemoryStream;
            var second = subject.Resolve<IDisposable>() as MemoryStream;

            Assert.NotNull(first);
            Assert.NotNull(second);
            Assert.AreNotSame(first, second);
        }