Esempio n. 1
0
        public void ShouldThrowExceptionIfAddSameTypeTwice()
        {
            IContainer container = new SimpleContainer();

            container.Add <MyThing>();
            container.Add <MyThing>();
            Assert.Fail("Should have thrown exception");
        }
Esempio n. 2
0
        public void ShouldResolveDependencies()
        {
            IContainer container = new SimpleContainer();

            container.Add <MyDependency>();
            container.Add <ThingWithNoDependencies>();

            var myThing = container.Resolve <MyDependency>();

            Assert.That(myThing.Dependency, Is.InstanceOfType(typeof(ThingWithNoDependencies)));
        }
Esempio n. 3
0
        public void Run()
        {
            var l1 = new Leaf();
            var l2 = new Leaf();

            var c = new SimpleContainer();

            c.Add(l1);
            c.Add(l2);

            c.Method();
        }
Esempio n. 4
0
        public void ShouldResolveUsingConstructorWithMostDependencies()
        {
            IContainer container = new SimpleContainer();

            container.Add <MyThingWithReverseConstructor>();
            container.Add <ThingWithNoDependencies>();

            var myThing = container.Resolve <MyThingWithReverseConstructor>();

            Assert.That(myThing.Dependency, Is.Not.Null, "Wrong constructor was used");
            Assert.That(myThing.Dependency, Is.InstanceOfType(typeof(ThingWithNoDependencies)));
        }
Esempio n. 5
0
        public void ShouldResolveWithDependenciesInAnyOrder()
        {
            IContainer container = new SimpleContainer();

            container.Add <MyDependency>();
            container.Add <MyThing>();
            container.Add <ThingWithNoDependencies>();

            var myThing = container.Resolve <MyThing>();

            Assert.That(myThing.Dependency, Is.InstanceOfType(typeof(MyDependency)), "1st level Dependency was not fulfilled");
            Assert.That(myThing.Dependency.Dependency, Is.InstanceOfType(typeof(ThingWithNoDependencies)), "2nd level Dependency was not fulfiled");
        }
Esempio n. 6
0
        public void Add_ExistingKey_ObjectIsOverrided()
        {
            var container  = new SimpleContainer();
            var container2 = new SimpleContainer();

            //act
            container.Add(container);
            container.Add(container2);

            var obj = container.Resolve <SimpleContainer>();

            //assert
            Assert.Same(container2, obj);
        }
Esempio n. 7
0
        public void ResolveShouldThrowExceptionIfConstructorIsNotSatifiable()
        {
            IContainer container = new SimpleContainer();

            container.Add <MyThing>();
            container.Resolve <MyThing>();
            Assert.Fail("Should have thrown exception");
        }
Esempio n. 8
0
        public void ShouldResolveByType()
        {
            IContainer container = new SimpleContainer();

            container.Add <IThing, ThingWithNoDependencies>();

            object thing = container.Resolve(typeof(IThing));

            Assert.That(thing, Is.InstanceOfType(typeof(ThingWithNoDependencies)));
        }
Esempio n. 9
0
        public void ShouldResolveUsingConstructorWithMostDependenciesThatIsSatisfiable()
        {
            IContainer container = new SimpleContainer();

            container.Add <MyThingWithReverseConstructor>();

            var myThing = container.Resolve <MyThingWithReverseConstructor>();

            Assert.That(myThing.Dependency, Is.Null);
        }
Esempio n. 10
0
        public void ShouldAddAndResolveByClass()
        {
            IContainer container = new SimpleContainer();

            container.Add <ThingWithNoDependencies>();

            var result = container.Resolve <ThingWithNoDependencies>();

            Assert.That(result, Is.InstanceOfType(typeof(ThingWithNoDependencies)));
        }
Esempio n. 11
0
        public void ShouldAddAndResolveByInterface()
        {
            IContainer container = new SimpleContainer();

            container.Add <IThing, ThingWithNoDependencies>();

            var thing = container.Resolve <IThing>();

            Assert.That(thing, Is.InstanceOfType(typeof(ThingWithNoDependencies)));
        }
Esempio n. 12
0
        public void ResolveShouldReturnSameInstanceWhenCalledTwice()
        {
            IContainer container = new SimpleContainer();

            container.Add <ThingWithNoDependencies>();

            var result1 = container.Resolve <ThingWithNoDependencies>();
            var result2 = container.Resolve <ThingWithNoDependencies>();

            Assert.AreSame(result1, result2);
        }
Esempio n. 13
0
        public void ShouldChainContainersThroughMissingAction()
        {
            IContainer parent = new SimpleContainer();

            parent.Add <IThing, ThingWithNoDependencies>();

            IContainer child = new SimpleContainer(parent.Resolve);

            IThing thing = child.Resolve <IThing>();

            Assert.That(thing, Is.InstanceOfType(typeof(ThingWithNoDependencies)));
        }
Esempio n. 14
0
        public void ShouldDecorateAnExistingComponent()
        {
            IContainer container = new SimpleContainer();

            container.Add <IThing, ThingWithNoDependencies>();
            container.Decorate <IThing, DecoratedThing>();

            var thing = container.Resolve <IThing>();

            Assert.That(thing, Is.InstanceOfType(typeof(DecoratedThing)));
            Assert.That(thing.Dependency, Is.InstanceOfType(typeof(ThingWithNoDependencies)));
        }
Esempio n. 15
0
        public void Add_NewKey_ObjectAdded()
        {
            var container = new SimpleContainer();

            //act
            container.Add(container);

            var obj = container.Resolve <SimpleContainer>();

            //assert
            Assert.Same(container, obj);
        }
Esempio n. 16
0
        public void Resolve_NonGeneric_ReturnCorrectObject()
        {
            var container = new SimpleContainer();

            container.Add(container);

            //act
            var obj = container.Resolve(typeof(SimpleContainer));

            //assert
            Assert.Same(container, obj);
        }
Esempio n. 17
0
        public void ShouldOnlyCallCreationLambdaOnce()
        {
            int        count     = 0;
            IContainer container = new SimpleContainer();

            container.Add <IThing>(() => {
                count++;
                return(new ThingWithNoDependencies());
            });

            container.Resolve <IThing>();
            container.Resolve <IThing>();

            Assert.That(count, Is.EqualTo(1));
        }
Esempio n. 18
0
        public void ShouldOnlyCallCreationLambdaOnceEvenFromDifferentThreads()
        {
            int        count     = 0;
            IContainer container = new SimpleContainer();

            container.Add <IThing>(() => {
                count++;
                Thread.Sleep(10);
                return(new ThingWithNoDependencies());
            });

            IThing[] results = new IThing[2];
            ThreadPool.QueueUserWorkItem((ignore) => results[0] = container.Resolve <IThing>());
            ThreadPool.QueueUserWorkItem((ignore) => results[1] = container.Resolve <IThing>());

            Thread.Sleep(50);

            Assert.That(count, Is.EqualTo(1));
            Assert.AreSame(results[0], results[1]);
        }
Esempio n. 19
0
        public void TestBuilder_WithContainer_ObjectIsCreatedUsingBuilderProperties()
        {
            var container   = new SimpleContainer();
            var testService = new TestServiceA();

            container.Add <ITestServiceA>(testService);

            var builder = new Implementation.BuilderWithOneInterfaceProperty(container);

            var testObject = builder.Build();

            //Assert the builder properties
            Assert.Same(testService, builder.TestService); //use the object from container
            Assert.Equal(2, builder.Id);

            //Assert the object properties
            Assert.Equal(0, testObject.Number);
            Assert.Equal(DateTime.MinValue, testObject.Date);
            Assert.Same(builder.TestService, testObject.TestServiceA); //use the same object from container
            Assert.NotNull(testObject.TestObjectB);                    //create a new object
        }