Ejemplo n.º 1
0
        public void Cyclic_DependencyOverride()
        {
            // Arrange
            Container.RegisterType <I0, G0>()
            .RegisterType <I1, G1>();

            //next line throws StackOverflowException
            Container.Resolve <G1>(
                Override.Dependency <I0>(
                    Resolve.Dependency <I1>()));
        }
Ejemplo n.º 2
0
        public void CtorParameter()
        {
            // Arrange
            Container.RegisterType <Service>(Invoke.Constructor(_data));

            // Act
            var value = Container.Resolve <Service>(Override.Dependency <string>(_override));

            // Verify
            Assert.AreSame(_data, value.Data);
        }
        public void Override_NamedDependency()
        {
            // Arrange
            Container.RegisterInstance(_data)
            .RegisterInstance(Name, Name);

            // Act
            var instance = Container.Resolve <CtorWithNamedDependency>(Override.Dependency <string>(Name, _override));

            // Validate
            Assert.AreEqual(_override, instance.Data);
        }
Ejemplo n.º 4
0
        public void Overrides_DependencyOverrideFieldValue()
        {
            var other = "other";

            // Act
            var result = Container.Resolve <ObjectWithAttributes>(
                Override.Field(nameof(ObjectWithAttributes.Dependency), null),
                Override.Dependency(other, other));

            // Verify
            Assert.IsNotNull(result);
            Assert.IsNull(result.Dependency);
            Assert.IsNotNull(result.Optional);
            Assert.AreEqual(result.Optional, other);
        }
        public void OptionalViaDependencyAsType()
        {
            // Setup
            IService x = new Service1();
            IService y = new Service2();

            // Act
            var result = Container.Resolve <Foo>(
                Override.Dependency(typeof(IService), "Fred", x),
                Override.Dependency(typeof(IService), "George", y));

            // Verify
            Assert.IsNotNull(result);
            Assert.IsNotNull(result.Fred);
            Assert.IsNotNull(result.George);
        }
        public void InjectedParameterWithDependencyOverride()
        {
            // Setup
            var noOverride  = "default";
            var depOverride = "custom-via-override";

            Container.RegisterType <TestType>(Invoke.Constructor(noOverride));
            // Act
            var defaultValue = Container.Resolve <TestType>().ToString();
            var depValue     = Container.Resolve <TestType>(Override.Dependency <string>(depOverride)).ToString();
            var propValue    = Container.Resolve <TestType>(Override.Parameter <string>(depOverride)).ToString();

            // Verify
            Assert.AreSame(noOverride, defaultValue);
            Assert.AreSame(noOverride, depValue);
            Assert.AreSame(depOverride, propValue);
        }
Ejemplo n.º 7
0
        public void Overrides_DependencyOverrideFieldValueResolver()
        {
            var other     = "other";
            var resolver1 = new ValidatingResolver(Name);

            // Act
            var result = Container.Resolve <ObjectWithAttributes>(
                Override.Field(nameof(ObjectWithAttributes.Dependency), resolver1),
                Override.Dependency(other, other));

            // Verify
            Assert.IsNotNull(result);
            Assert.AreEqual(result.Dependency, Name);
            Assert.AreEqual(result.Optional, other);

            Assert.AreEqual(typeof(string), resolver1.Type);
        }
        public void InjectedPropertyWithDependencyOverride()
        {
            // Setup
            var noOverride  = "default";
            var depOverride = "custom-via-override";

            Container.RegisterType <TestType>(Inject.Property(nameof(TestType.DependencyProperty), noOverride));

            // Act
            var defaultValue = Container.Resolve <TestType>().DependencyProperty;
            var depValue     = Container.Resolve <TestType>(Override.Dependency <string>(depOverride)).DependencyProperty;
            var propValue    = Container.Resolve <TestType>(Override.Property(nameof(TestType.DependencyProperty), depOverride)).DependencyProperty;

            // Verify
            Assert.AreSame(noOverride, defaultValue);
            Assert.AreSame(noOverride, depValue);
            Assert.AreSame(depOverride, propValue);
        }
        public void InjectedFieldWithFielDependencyOverride()
        {
            // Setup
            var noOverride  = "default";
            var depOverride = "custom-via-override";

            Container.RegisterType <TestType>(Inject.Field(nameof(TestType.DependencyField), noOverride));

            // Act
            var defaultValue = Container.Resolve <TestType>().DependencyField;
            var dependValue  = Container.Resolve <TestType>(Override.Dependency <string>(depOverride)).DependencyField;
            var fieldValue   = Container.Resolve <TestType>(Override.Field(nameof(TestType.DependencyField), depOverride)).DependencyField;

            // Verify
            Assert.AreSame(noOverride, defaultValue);
            Assert.AreSame(noOverride, dependValue);
            Assert.AreSame(depOverride, fieldValue);
        }
Ejemplo n.º 10
0
        // https://github.com/unitycontainer/container/issues/140
        public void Issue_Container_140()
        {
            // Setup
            var noOverride  = "default";
            var parOverride = "custom-via-parameteroverride";
            var depOverride = "custom-via-dependencyoverride";

            Container.RegisterType <Foo>(new InjectionConstructor(noOverride));
            // Act
            var defaultValue = Container.Resolve <Foo>().ToString();
            var depValue     = Container.Resolve <Foo>(Override.Dependency <string>(depOverride)).ToString();
            var parValue     = Container.Resolve <Foo>(Override.Parameter <string>("dependency", parOverride))
                               .ToString();

            // Verify
            Assert.AreSame(noOverride, defaultValue);
            Assert.AreSame(parOverride, parValue);
            Assert.AreSame(depOverride, depValue);
        }