Пример #1
0
        public void Container_Returns_Service_When_Registered_With_No_Key_But_Accessed_With_Key()
        {
            //Arrange
            var container = new Container();
            var mock = new Mock<IService>();
            var inst = mock.Object;

            //Act
            container.Register(typeof(IService), null, (c, t, k) => inst);
            var inst1 = container.GetInstance<IService>("key");

            //Assert
            Assert.AreSame(inst, inst1);
        }
Пример #2
0
        public void Container_RegisterSingleton_Returns_Null_If_Container_Disposed()
        {
            //Arrange
            var container = new Container();
            var mock = new Mock<IDisposable>();
            var inst = mock.Object;

            //Act
            container.RegisterSingleton(typeof(IDisposable), "key", inst);
            container.Dispose();
            var inst1 = container.GetInstance<IDisposable>("key");

            //Assert
            Assert.IsNull(inst1);
        }
Пример #3
0
        public void Container_RegisterInstance_T_Returns_Same_Instance()
        {
            //Arrange
            var container = new Container();
            var mock = new Mock<IService>();
            var inst = mock.Object;

            //Act
            container.RegisterInstance<IService>("key", inst);
            var inst1 = container.GetInstance<IService>("key");
            var inst2 = container.GetInstance<IService>("key");

            //Assert
            Assert.AreSame(inst, inst1);
            Assert.AreSame(inst, inst2);
        }
Пример #4
0
        public void Container_Returns_Correct_Instance_When_Using_Key()
        {
            //Arrange
            var container = new Container();
            var mocka = new Mock<IService>();
            var insta = mocka.Object;
            var mockb = new Mock<IService>();
            var instb = mockb.Object;

            //Act
            container.Register(typeof(IService), "keya", (c, t, k) => insta);
            container.Register(typeof(IService), "keyb", (c, t, k) => instb);
            var inst1 = container.GetInstance<IService>("keya");
            var inst2 = container.GetInstance<IService>("keyb");

            //Assert
            Assert.AreSame(insta, inst1);
            Assert.AreSame(instb, inst2);
            Assert.AreNotSame(instb, inst1);
            Assert.AreNotSame(insta, inst2);
        }
Пример #5
0
        public void Container_Register_T_Supports_Transient_Lifestyle()
        {
            //Arrange
            var container = new Container();

            //Act
            container.Register<IService>("key", (c, t, k) =>
                                                        {
                                                            var mock = new Mock<IService>();
                                                            return mock.Object;
                                                        });

            var inst1 = container.GetInstance<IService>("key");
            var inst2 = container.GetInstance<IService>("key");

            //Assert
            Assert.AreNotSame(inst1, inst2);
        }
Пример #6
0
        public void Container_Register_T_Supports_Singleton_Lifestyle()
        {
            //Arrange
            var container = new Container();
            var mock = new Mock<IService>();
            var inst = mock.Object;

            //Act
            container.Register<IService>("key", (c, t, k) => inst);
            var inst1 = container.GetInstance<IService>("key");
            var inst2 = container.GetInstance<IService>("key");

            //Assert
            Assert.AreSame(inst, inst1);
            Assert.AreSame(inst, inst2);
        }
Пример #7
0
        public void Container_RegisterType_Returns_Instance()
        {
            //Arrange
            var container = new Container();
            var mock = new Mock<IService>();
            var inst = mock.Object;
            container.Register<IService>((c, t, k) => inst);

            //Act
            container.RegisterType(typeof(IService2), null, typeof(Service2Impl));
            var svc = container.GetInstance<IService2>();

            //Assert
            Assert.AreSame(inst, svc.Service);
        }
Пример #8
0
        public void Container_Supports_Injection()
        {
            //Arrange
            var container = new Container();
            var mock = new Mock<IService>();
            var inst = mock.Object;

            //Act
            container.Register(typeof(IService2), null, (c, t, k) => new Service2Impl(c.GetInstance<IService>()));
            container.Register(typeof(IService), null, (c, t, k) => inst);
            var svc = container.GetInstance<IService2>();

            //Assert
            Assert.AreSame(inst, svc.Service);
        }
Пример #9
0
        public void Container_Uses_Last_Builder_When_Multiple_Instances_Of_Same_Service_Registered()
        {
            //Arrange
            var container = new Container();
            var mock1 = new Mock<IService>();
            mock1.Setup(m => m.Id).Returns(1);
            var inst1 = mock1.Object;

            var mock2 = new Mock<IService>();
            mock2.Setup(m => m.Id).Returns(2);
            var inst2 = mock2.Object;

            //Act
            container.Register(typeof(IService), null, (c, t, k) => inst1);
            container.Register(typeof(IService), null, (c, t, k) => inst2);
            var inst3 = container.GetInstance<IService>();

            //Assert
            Assert.AreNotEqual(inst1.Id, inst3.Id);
            Assert.AreEqual(inst2.Id, inst3.Id);
        }
Пример #10
0
        public void Container_Throws_If_Injection_Without_Dependency()
        {
            //Arrange
            var container = new Container();

            container.Register(typeof(IService2), null, (c, t, k) => new Service2Impl(c.GetInstance<IService>()));
            var svc = container.GetInstance<IService2>();
        }
Пример #11
0
        public void Container_Thows_When_Registered_With_Key_But_Accessed_With_No_Key()
        {
            //Arrange
            var container = new Container();
            var mock = new Mock<IService>();
            var inst = mock.Object;

            //Act
            container.Register(typeof(IService), "key", (c, t, k) => inst);
            var inst1 = container.GetInstance<IService>();
        }
Пример #12
0
        public void Container_Supports_PerThread_Lifestyle()
        {
            //Arrange
            var container = new Container();

            //Act
            container.Register(typeof(IService), null, (c, t, k) => servicePerThread);
            var id = container.GetInstance<IService>().Id;
            var thread = new Thread(() =>
            {
                try
                {
                    var id2 = container.GetInstance<IService>().Id;

                    //Assert
                    Assert.AreNotEqual(id, id2);
                }
                catch (Exception ex)
                {
                    Assert.Fail(ex.ToString());
                }
            });
            thread.Start();
            thread.Join();
        }