Ejemplo n.º 1
0
        public void GivenCreate_WhenConfigureAsSingleton_ThenCreatesOneObjectForEntireDILifecycle()
        {
            MockConfigureSpecify <IBasicClass, BasicClass>(ConfigurationScope.Singleton);
            var dependencyInjector = new DependencyInjector(_mockConfiguration);

            var result       = dependencyInjector.Create <IBasicClass>();
            var secondResult = dependencyInjector.Create <IBasicClass>();

            Assert.AreEqual(result, secondResult);
        }
Ejemplo n.º 2
0
        public void GivenCreate_WhenConfigureAsTransient_ThenCreatesNewObjectObjectEachTime()
        {
            MockConfigureSpecify <IBasicClass, BasicClass>();
            MockConfigureSpecify <IComplexClass, ComplexClass>();
            MockConfigureSpecify <IMoreComplexClass, MoreComplexClass>();

            var dependencyInjector = new DependencyInjector(_mockConfiguration);

            var moreComplexClass        = (IMoreComplexClass)dependencyInjector.Create <IMoreComplexClass>();
            var anotherMoreComplexClass = (IMoreComplexClass)dependencyInjector.Create <IMoreComplexClass>();

            Assert.AreNotEqual(moreComplexClass, anotherMoreComplexClass);
            Assert.AreNotEqual(moreComplexClass.MyBasicClass, anotherMoreComplexClass.MyBasicClass);
            Assert.AreNotEqual(moreComplexClass.MyBasicClass, moreComplexClass.MyComplexClass.MyBasicClass);
        }
Ejemplo n.º 3
0
        public void GivenCreate_WhenConfigureAsScoped_ThenCreatesOneObjectForRequestFlow()
        {
            MockConfigureSpecify <IBasicClass, BasicClass>(ConfigurationScope.Scoped);
            MockConfigureSpecify <IComplexClass, ComplexClass>(ConfigurationScope.Scoped);
            MockConfigureSpecify <IMoreComplexClass, MoreComplexClass>(ConfigurationScope.Scoped);

            var dependencyInjector = new DependencyInjector(_mockConfiguration);

            var moreComplexClass        = (IMoreComplexClass)dependencyInjector.Create <IMoreComplexClass>();
            var anotherMoreComplexClass = (IMoreComplexClass)dependencyInjector.Create <IMoreComplexClass>();

            Assert.AreNotEqual(moreComplexClass, anotherMoreComplexClass);
            Assert.AreNotEqual(moreComplexClass.MyBasicClass, anotherMoreComplexClass.MyBasicClass);
            Assert.AreEqual(moreComplexClass.MyBasicClass, moreComplexClass.MyComplexClass.MyBasicClass);
        }
Ejemplo n.º 4
0
        public void GivenCreate_WhenConfigureBaseClassSpecifyBasicSubClass_ThenCreatesClass()
        {
            MockConfigureSpecify <BasicClass, BasicSubClass>();
            var dependencyInjector = new DependencyInjector(_mockConfiguration);

            var result = dependencyInjector.Create <BasicClass>();

            Assert.NotNull(result);
            Assert.AreEqual(typeof(BasicSubClass), result.GetType());
        }
Ejemplo n.º 5
0
        public void GivenCreate_WhenConfigureInterfaceSpecifyBasicClass_ThenCreatesClass()
        {
            MockConfigureSpecify <IBasicClass, BasicClass>();
            var dependencyInjector = new DependencyInjector(_mockConfiguration);

            var basicClass = dependencyInjector.Create <IBasicClass>();

            Assert.NotNull(basicClass);
            Assert.AreEqual(typeof(BasicClass), basicClass.GetType());
        }
Ejemplo n.º 6
0
        public void GivenCreate_WhenConfigurationIsConstructorFunction_ThenCreatesClass()
        {
            MockConfigureConstructorFunction <IBasicClass>(() => new BasicClass());
            var dependencyInjector = new DependencyInjector(_mockConfiguration);

            var basicClass = dependencyInjector.Create <IBasicClass>();

            Assert.NotNull(basicClass);
            Assert.AreEqual(typeof(BasicClass), basicClass.GetType());
        }
Ejemplo n.º 7
0
        public void GivenCreate_WhenSingleDependencyClassIsSpecifiedAndDependencyIsConfigured_ThenCreatesClass()
        {
            MockConfigureSpecify <IBasicClass, BasicClass>();
            MockConfigureSpecify <IComplexClass, ComplexClass>();

            var dependencyInjector = new DependencyInjector(_mockConfiguration);

            var result = dependencyInjector.Create <IComplexClass>();

            Assert.NotNull(result);
        }
Ejemplo n.º 8
0
        public void GivenCreate_WhenSingleDependencyClassIsSpecifiedAndDependencyNotConfigured_ThenThrowsException()
        {
            MockConfigureSpecify <IComplexClass, ComplexClass>();
            var dependencyInjector = new DependencyInjector(_mockConfiguration);

            try
            {
                dependencyInjector.Create <IComplexClass>();
                Assert.True(false);
            }
            catch (ArgumentException ex)
            {
                Assert.True(true);
                Assert.True(ex.Message.Contains("Dependency of"));
            }
        }
Ejemplo n.º 9
0
        public void GivenCreate_WhenConfigurationStyleIsNone_ThenCreatesClass()
        {
            var configurationType = typeof(IBasicClass);
            var injection         = new InjectionSpecification
            {
                ConfigurationType  = configurationType,
                ConfigurationStyle = ConfigurationStyle.None
            };

            A.CallTo(() => _mockConfiguration.IsConfigured(configurationType)).Returns(true);
            A.CallTo(() => _mockConfiguration.GetInjectionSpecification(configurationType)).Returns(injection);

            var dependencyInjector = new DependencyInjector(_mockConfiguration);

            Assert.Throws <ArgumentException>(() => dependencyInjector.Create <IBasicClass>());
        }
Ejemplo n.º 10
0
        public void GivenCreate_WhenConfigurationIsNotSpecified_ThenThrowsArgumentException()
        {
            var dependencyInjector = new DependencyInjector();

            Assert.Throws <ArgumentException>(() => dependencyInjector.Create <IBasicClass>());
        }