public void CreateHybrid_WithValidScopedLifestyles_ReturnsScopedLifestyle()
        {
            // Arrange
            ScopedLifestyle trueLifestyle  = new CustomScopedLifestyle();
            ScopedLifestyle falseLifestyle = new CustomScopedLifestyle();

            // Act
            ScopedLifestyle hybrid = Lifestyle.CreateHybrid(() => true, trueLifestyle, falseLifestyle);

            // Assert
            Assert.IsNotNull(hybrid);
        }
        public void RegisterForDisposal_CalledOnCreatedScopedHybridWithTrueSelector_ForwardsCallToTrueLifestyle()
        {
            // Arrange
            CustomScopedLifestyle trueLifestyle  = new CustomScopedLifestyle();
            CustomScopedLifestyle falseLifestyle = new CustomScopedLifestyle();

            ScopedLifestyle hybrid = Lifestyle.CreateHybrid(() => true, trueLifestyle, falseLifestyle);

            // Act
            hybrid.RegisterForDisposal(new Container(), new DisposableObject());

            // Assert
            Assert.IsTrue(trueLifestyle.ScopeUsed, "TrueLifestyle was expected to be called.");
            Assert.IsFalse(falseLifestyle.ScopeUsed, "FalseLifestyle was NOT expected to be called.");
        }
        public void WhenScopeEnds_CalledOnCreatedScopedHybridWithTrueSelector_ForwardsCallToTrueLifestyle()
        {
            // Arrange
            CustomScopedLifestyle trueLifestyle  = new CustomScopedLifestyle();
            CustomScopedLifestyle falseLifestyle = new CustomScopedLifestyle();

            ScopedLifestyle hybrid = Lifestyle.CreateHybrid(() => true, trueLifestyle, falseLifestyle);

            // Act
            hybrid.WhenScopeEnds(new Container(), () => { });

            // Assert
            Assert.IsTrue(trueLifestyle.ScopeUsed, "TrueLifestyle was expected to be called.");
            Assert.IsFalse(falseLifestyle.ScopeUsed, "FalseLifestyle was NOT expected to be called.");
        }
Beispiel #4
0
        public void RegisterForDisposal_CalledOnCreatedScopedHybridWithFalseSelector_ForwardsCallToFalseLifestyle()
        {
            // Arrange
            CustomScopedLifestyle trueLifestyle  = new CustomScopedLifestyle();
            CustomScopedLifestyle falseLifestyle = new CustomScopedLifestyle();

            ScopedLifestyle hybrid = Lifestyle.CreateHybrid(() => false, trueLifestyle, falseLifestyle);

            // Act
            hybrid.RegisterForDisposal(new Container(), new DisposableObject());

            // Assert
            Assert.AreEqual(0, trueLifestyle.RegisterForDisposalCallCount, "TrueLifestyle was NOT expected to be called.");
            Assert.AreEqual(1, falseLifestyle.RegisterForDisposalCallCount, "FalseLifestyle was expected to be called.");
        }
Beispiel #5
0
        public void WhenScopeEnds_CalledOnCreatedScopedHybridWithFalseSelector_ForwardsCallToFalseLifestyle()
        {
            // Arrange
            CustomScopedLifestyle trueLifestyle  = new CustomScopedLifestyle();
            CustomScopedLifestyle falseLifestyle = new CustomScopedLifestyle();

            ScopedLifestyle hybrid = Lifestyle.CreateHybrid(() => false, trueLifestyle, falseLifestyle);

            // Act
            hybrid.WhenScopeEnds(new Container(), () => { });

            // Assert
            Assert.AreEqual(0, trueLifestyle.WhenScopeEndsCallCount, "TrueLifestyle was NOT expected to be called.");
            Assert.AreEqual(1, falseLifestyle.WhenScopeEndsCallCount, "FalseLifestyle was expected to be called.");
        }
Beispiel #6
0
        public void DependencyHasPossibleLifestyleMismatch_ShortScopedHybridToLongScopedHybrid_ReportsMismatch()
        {
            // Arrange
            ScopedLifestyle singleton = new CustomScopedLifestyle(Lifestyle.Singleton);
            ScopedLifestyle transient = new CustomScopedLifestyle(Lifestyle.Transient);

            var parentHybrid = Lifestyle.CreateHybrid(() => true, singleton, singleton);
            var childHybrid  = Lifestyle.CreateHybrid(() => true, singleton, transient);

            var dependency = CreateRelationship(parent: parentHybrid, child: childHybrid);

            // Act
            bool result = HasPossibleLifestyleMismatch(dependency);

            // Assert
            Assert.IsTrue(result, "Both lifestyles of the parent are longer than those of the child.");
        }
        public void RegisterForDisposal_CalledOnCreatedScopedHybridWithTrueSelector_ForwardsCallToTrueLifestyleScope()
        {
            // Arrange
            Scope trueScope  = new Scope();
            Scope falseScope = new Scope();

            var trueLifestyle  = new CustomScopedLifestyle(trueScope);
            var falseLifestyle = new CustomScopedLifestyle(falseScope);

            ScopedLifestyle hybrid = Lifestyle.CreateHybrid(() => true, trueLifestyle, falseLifestyle);

            // Act
            hybrid.RegisterForDisposal(new Container(), new DisposableObject());

            // Assert
            Assert.AreEqual(1, trueScope.GetDisposables().Length, "TrueLifestyle was expected to be called.");
            Assert.IsFalse(falseScope.GetDisposables().Any(), "FalseLifestyle was NOT expected to be called.");
        }
        public void GetInstance_CalledMultipleTimesForDifferentLifestyles_ResolvesInstanceForBothLifestyles()
        {
            // Arrange
            var container = new Container();

            bool selectTrueLifestyle = true;

            Scope trueScope  = new Scope(container);
            Scope falseScope = new Scope(container);

            var trueLifestyle  = new CustomScopedLifestyle(trueScope);
            var falseLifestyle = new CustomScopedLifestyle(falseScope);

            ScopedLifestyle hybrid =
                Lifestyle.CreateHybrid(() => selectTrueLifestyle, trueLifestyle, falseLifestyle);

            container.Register <IDisposable, DisposableObject>(hybrid);

            // Act
            var instance1 = container.GetInstance <IDisposable>();

            // Assert
            Assert.AreEqual(1, trueScope.GetDisposables().Length, "TrueLifestyle was expected to be called.");
            Assert.IsFalse(falseScope.GetDisposables().Any(), "FalseLifestyle was NOT expected to be called.");

            // Act
            // Here we flip the switch. Resolving the instance now should get a new instance from the other
            // lifestyle
            selectTrueLifestyle = false;

            var instance2 = container.GetInstance <IDisposable>();

            // Assert
            Assert.AreEqual(1, falseScope.GetDisposables().Length, "FalseLifestyle was expected to be called this time.");
            Assert.IsFalse(object.ReferenceEquals(instance1, instance2));
        }