예제 #1
0
            public void ReturnsTrueWhenTheTypeRequestedIsTheBaseTypeOfTheConcreteInstance()
            {
                var container = new AutoContainer(new Foo(0));

                var result = container.CanGet(typeof(FooBase));

                Assert.That(result, Is.True);
            }
예제 #2
0
            public void ReturnsTrueWhenTheTypeRequestedIsAnInterfaceThatTheConcreteInstanceInheritsFrom()
            {
                var container = new AutoContainer(new Foo(0));

                var result = container.CanGet(typeof(IFoo));

                Assert.That(result, Is.True);
            }
예제 #3
0
            public void ReturnsTrueWhenTheTypeRequestedIsObject()
            {
                var conatiner = new AutoContainer(new Foo(0));

                var result = conatiner.CanGet(typeof(object));

                Assert.That(result, Is.True);
            }
예제 #4
0
            public void ReturnsFalseWhenTheTypeRequestedHasNoResolvableConstructors()
            {
                var container = new AutoContainer();

                var result = container.CanGet(typeof(NeedsAnIFoo));

                Assert.That(result, Is.False);
            }
예제 #5
0
            public void ReturnsFalseWhenTheTypeRequestedIsEntirelyUnrelatedToAnyOfTheConcreteInstances()
            {
                var container = new AutoContainer(new Foo(0));

                var result = container.CanGet(typeof(IIsNotIFoo));

                Assert.That(result, Is.False);
            }
예제 #6
0
            public void ReturnsFalseWhenTheTypeRequestedTypeHasBeenNullified()
            {
                // Foo and AnotherFoo both implement IFoo, so IFoo becomes ineligible.
                var container = new AutoContainer(new Foo(0), new AnotherFoo());

                var result = container.CanGet(typeof(IFoo));

                Assert.That(result, Is.False);
            }
예제 #7
0
            public void WhenTheRequestedTypeIsResolvableButNotToOneOfTheConcreteInstancesThenANewInstanceIsReturnedEveryTime()
            {
                _container = new AutoContainer(new Foo(0));

                var first  = Get <NeedsAnIFoo>();
                var second = Get <NeedsAnIFoo>();

                Assert.That(first, Is.Not.SameAs(second));
            }
예제 #8
0
            public void WhenTheRequestedTypeResolvesToOneOfTheConcreteInstancesThenTheSameInstanceIsReturnedEveryTime()
            {
                _container = new AutoContainer(new Foo(0));

                var first  = Get <IFoo>();
                var second = Get <IFoo>();

                Assert.That(first, Is.SameAs(second));
            }
예제 #9
0
                public void ThatThrowsAnExceptionIfNeitherTheBaseAutoContainerNorTheSecondaryResolverCanResolve()
                {
                    var foo        = new Foo(0);
                    var anotherFoo = new AnotherFoo();

                    var primary   = new AutoContainer(foo);
                    var secondary = new AutoContainer(anotherFoo);

                    _mergedContainer = primary.MergeWith(secondary);

                    Assert.That(() => Get <NeedsAnIFooAndAnIBar>(), Throws.Exception);
                }
예제 #10
0
            public void ReturnsTrueWhenTheTypeRequestedIsTheTypeOfAConcreteInstance()
            {
                // Foo and AnotherFoo implement IFoo and Foo and YetAnotherFoo inherit FooBase,
                // making IFoo and FooBase ineligible. Nothing else in Foo's type hierarchy
                // other than the Foo type itself is still eligible. And Foo itself cannot be
                // resolved because of its constructor. So the only way we'll be able to get a
                // Foo from the container is by asking for a Foo.
                var container = new AutoContainer(new Foo(0), new AnotherFoo(), new YetAnotherFoo());

                var result = container.CanGet(typeof(Foo));

                Assert.That(result, Is.True);
            }
        public void UsesValueFromIResolverIfNoMatchForParameterIsFound()
        {
            var fooContainer = Deserialize <FooContainer6>(_externalDependency);

            Assert.That(fooContainer.Foo, Is.InstanceOf <XmlDeserializationProxy <IFoo6> >());

            var autoContainer = new AutoContainer(new Baz6());
            var foo           = fooContainer.Foo.CreateInstance(autoContainer);

            Assert.That(foo, Is.InstanceOf <Foo6>());

            Assert.That(foo.GetBar(), Is.InstanceOf <Bar6>());
            Assert.That(foo.GetBaz(), Is.InstanceOf <Baz6>());
        }
예제 #12
0
                public void ThatReturnsFalseIfTheCanGetMethodFromBothTheBaseAutoContainerAndSecondaryResolverReturnFalse()
                {
                    IResolver primary   = new AutoContainer();
                    IResolver secondary = new AutoContainer();

                    // Sanity checks
                    Assert.That(primary.CanGet(typeof(NeedsAnIFoo)), Is.False);
                    Assert.That(secondary.CanGet(typeof(NeedsAnIFoo)), Is.False);

                    var merged = primary.MergeWith(secondary);

                    var result = merged.CanGet(typeof(NeedsAnIFoo));

                    Assert.That(result, Is.False);
                }
예제 #13
0
                public void ThatReturnsWhatTheSecondaryResolverReturnsIfTheBaseAutoContainerCannotResolve()
                {
                    var foo        = new Foo(0);
                    var bar        = new Bar();
                    var anotherFoo = new AnotherFoo();

                    var primary   = new AutoContainer(foo);
                    var secondary = new AutoContainer(bar, anotherFoo);

                    _mergedContainer = primary.MergeWith(secondary);

                    var result = Get <NeedsAnIFooAndAnIBar>();

                    Assert.That(result.TheBar, Is.SameAs(bar));
                }
예제 #14
0
                public void ThatReturnsTrueIfTheCanGetMethodOfTheSecondaryResolverReturnsTrue()
                {
                    var foo = new Foo(0);

                    IResolver primary   = new AutoContainer();
                    IResolver secondary = new AutoContainer(foo);

                    // Sanity checks
                    Assert.That(primary.CanGet(typeof(NeedsAnIFoo)), Is.False);
                    Assert.That(secondary.CanGet(typeof(NeedsAnIFoo)), Is.True);

                    var merged = primary.MergeWith(secondary);

                    var result = merged.CanGet(typeof(NeedsAnIFoo));

                    Assert.That(result, Is.True);
                }
예제 #15
0
            public void PassesTheDefaultConstructorSelectorToTheOtherPublicConstructor()
            {
                try
                {
                    var mockConstructorSelector = new Mock <IResolverConstructorSelector>();

                    AutoContainer.UnlockDefaultResolverConstructorSelector();
                    AutoContainer.SetDefaultResolverConstructorSelector(mockConstructorSelector.Object);

                    var container = new AutoContainer();
                    container.CanGet(typeof(Foo)); // This method ends up accessing the constructor selector.

                    ConstructorInfo dummy;
                    mockConstructorSelector.Verify(
                        m => m.TryGetConstructor(It.Is <Type>(t => t == typeof(Foo)), It.IsAny <IResolver>(), out dummy));
                }
                finally
                {
                    AutoContainer.ResetDefaultResolverConstructorSelector();
                }
            }
예제 #16
0
            public void ThrowsAnExceptionWhenTheCanGetMethodReturnsFalse()
            {
                _container = new AutoContainer();

                Assert.That(() => Get <NeedsAnIFoo>(), Throws.Exception);
            }
예제 #17
0
            public void PassesTheInstancesCollectionToTheOtherPublicConstructor()
            {
                var container = new AutoContainer(new Foo(0));

                Assert.That(container.CanGet(typeof(Foo)), Is.True);
            }