public void Test_Error_ReAddingExistingBindings()
        {
            InjectorContext injector = new InjectorContext();

            //  Add two bindings
            injector.AddBinding <SimpleClassA>();
            injector.AddBinding <SimpleClassB>();

            //  Validate bindings
            Assert.Equal(2, injector.BindingCount);

            //  Check error
            Assert.Equal(0, injector.ErrorCount);

            //  Try re-adding first binding
            injector.AddBinding <SimpleClassA>();

            //  Check error
            Assert.Equal(1, injector.ErrorCount);
            Assert.Equal(InjectionErrorType.AlreadyAddedBindingForType, injector.GetError(0).error);

            //  Try re-adding second binding
            injector.AddBinding <SimpleClassB>();

            //  Check error
            Assert.Equal(2, injector.ErrorCount);
            Assert.Equal(InjectionErrorType.AlreadyAddedBindingForType, injector.GetError(1).error);
        }
示例#2
0
        public void Test_Error_TypedInjectionToWrongType()
        {
            InjectorContext injector = new InjectorContext();

            //  Add first binding
            injector.AddBinding <SimpleClassB>().ToType <SimpleClassB>();

            //  Create injection target
            var target = new ClassThatUses_SimpleClassA();

            //  Check before injection
            Assert.Null(target.value2);
            Assert.Null(target.value1);

            //  Inject
            injector.InjectInto(target);

            //  Check error
            Assert.Equal(2, injector.ErrorCount);
            Assert.Equal(InjectionErrorType.CanNotFindBindingForType, injector.GetError(0).error);
            Assert.Equal(InjectionErrorType.CanNotFindBindingForType, injector.GetError(1).error);

            //  Check after injection
            Assert.Null(target.value2);
            Assert.Null(target.value1);
        }
        public void Test_Error_AlreadyAddedValueProviderWithDifferentProvider()
        {
            InjectorContext injector = new InjectorContext();

            //  Add first binding to a value
            injector.AddBinding <ISimpleInterfaceAA>().ToType <SimpleClassA>();

            //  Check bindings and providers
            Assert.Equal(1, injector.BindingCount);
            Assert.Equal(1, injector.ProviderCount);

            //  Check errors
            Assert.Equal(0, injector.ErrorCount);

            //  Add second binding with same provider type
            injector.AddBinding <ISimpleInterfaceA>().ToValue(new SimpleClassA());

            //  Check bindings and providers
            Assert.Equal(2, injector.BindingCount);
            Assert.Equal(1, injector.ProviderCount);

            //  Check errors
            Assert.Equal(1, injector.ErrorCount);
            Assert.Equal(InjectionErrorType.AlreadyAddedTypeWithDifferentProvider, injector.GetError(0).error);
        }
示例#4
0
        public void Test_Error_BindingAfterInjectInto()
        {
            InjectorContext injector = new InjectorContext();

            //  Add a bindings
            injector.AddBinding <SimpleClassA>().ToType <SimpleClassA>();

            //  Validate bindings
            Assert.Equal(1, injector.BindingCount);

            //  Check error
            Assert.Equal(0, injector.ErrorCount);

            //  Get instance
            var value = new ClassThatUses_SimpleClassA();

            injector.InjectInto(value);

            //  Try adding a new binding
            injector.AddBinding <SimpleClassB>();

            //  Check error
            Assert.Equal(1, injector.ErrorCount);
            Assert.Equal(InjectionErrorType.BindingAfterInjection, injector.GetError(0).error);
        }
        public void Test_NamespaceRestriction()
        {
            InjectorContext injector = new InjectorContext();

            //  Add first
            injector.AddBinding <SimpleClassA>().ToType <SimpleClassA>().RestrictToNamespace("Com.Bit34Games.Injector.Test.Containers");

            //  Validate binding
            Assert.Equal(1, injector.BindingCount);
            Assert.True(injector.HasBindingForType(typeof(SimpleClassA)));

            //  Check error
            Assert.Equal(0, injector.ErrorCount);

            //  Inject
            var instanceA = new ClassThatUses_SimpleClassA();

            injector.InjectInto(instanceA);

            //  Check error
            Assert.Equal(0, injector.ErrorCount);

            //  Inject
            var instanceB = new ClassThatUses_SimpleClassA_InAnotherNamespace();

            injector.InjectInto(instanceB);

            //  Check error
            Assert.Equal(1, injector.ErrorCount);
            Assert.Equal(InjectionErrorType.InjectionRestricted, injector.GetError(0).error);
        }