public void RegisterForDisposal_WithValidArgumentsInHttpContext_Succeeds()
        {
            // Arrange
            ScopedLifestyle lifestyle = new WebRequestLifestyle();

            var         validContainer = new Container();
            IDisposable validInstance  = new DisposableCommand();

            using (new HttpContextScope())
            {
                // Act
                lifestyle.RegisterForDisposal(validContainer, validInstance);
            }
        }
Exemple #2
0
        public void RegisterForDisposal_OutsideTheContextOfAHttpRequest_ThrowsExpectedException()
        {
            // Arrange
            ScopedLifestyle lifestyle = new WebRequestLifestyle();

            var validContainer = new Container();
            var validInstance  = new DisposableCommand();

            // Act
            Action action = () => lifestyle.RegisterForDisposal(validContainer, validInstance);

            // Assert
            AssertThat.ThrowsWithExceptionMessageContains <InvalidOperationException>(
                "This method can only be called within the context of an active Web Request.", action);
        }
Exemple #3
0
        public void RegisterForDisposal_WithNullAction_ThrowsExpectedException()
        {
            // Arrange
            ScopedLifestyle lifestyle = new WebRequestLifestyle();

            IDisposable invalidInstance = null;

            using (new HttpContextScope())
            {
                // Act
                Action action = () => lifestyle.RegisterForDisposal(new Container(), invalidInstance);

                // Assert
                AssertThat.ThrowsWithParamName <ArgumentNullException>("disposable", action);
            }
        }
Exemple #4
0
        public void RegisterForDisposal_WithNullContainer_ThrowsExpectedException()
        {
            // Arrange
            ScopedLifestyle lifestyle = new WebRequestLifestyle();

            Container invalidContainer = null;

            using (new HttpContextScope())
            {
                // Act
                Action action = () => lifestyle.RegisterForDisposal(invalidContainer, new DisposableCommand());

                // Assert
                AssertThat.ThrowsWithParamName <ArgumentNullException>("container", action);
            }
        }
Exemple #5
0
        public void Verify_RegisterForDisposalCalledDuringVerificationOutsideAnHttpContext_Succeeds()
        {
            // Arrange
            ScopedLifestyle lifestyle = new WebRequestLifestyle();

            bool initializerCalled = false;

            var container = new Container();

            container.Register <DisposableCommand>();

            container.RegisterInitializer <DisposableCommand>(command =>
            {
                lifestyle.RegisterForDisposal(container, command);
                initializerCalled = true;
            });

            // Act
            container.Verify(VerificationOption.VerifyOnly);

            // Arrange
            Assert.IsTrue(initializerCalled);
        }
        public void WhenWebRequestEnds_BothAScopeEndsActionAndDisposableIntanceRegistered_InstancesGetDisposedLast()
        {
            // Arrange
            string expectedOrder = "[scope ending] [instance disposed]";

            string actualOrder = string.Empty;

            ScopedLifestyle lifestyle = new WebRequestLifestyle();

            var container = new Container();

            var scope = new HttpContextScope();

            try
            {
                lifestyle.RegisterForDisposal(container,
                    new DisposableAction(() => actualOrder += "[instance disposed]"));

                lifestyle.WhenScopeEnds(container, () => actualOrder += "[scope ending] ");
            }
            finally
            {
                // Act
                scope.Dispose();
            }

            // Assert
            Assert.AreEqual(expectedOrder, actualOrder,
                "Instances should get disposed after all 'scope ends' actions are executed, since those " +
                "delegates might still need to access those instances.");
        }
        public void RegisterForDisposal_WithValidArgumentsInHttpContext_Succeeds()
        {
            // Arrange
            ScopedLifestyle lifestyle = new WebRequestLifestyle();

            var validContainer = new Container();
            IDisposable validInstance = new DisposableCommand();

            using (new HttpContextScope())
            {
                // Act
                lifestyle.RegisterForDisposal(validContainer, validInstance);
            }
        }
        public void Verify_RegisterForDisposalCalledDuringVerificationOutsideAnHttpContext_Succeeds()
        {
            // Arrange
            ScopedLifestyle lifestyle = new WebRequestLifestyle();

            bool initializerCalled = false;

            var container = new Container();

            container.Register<DisposableCommand>();

            container.RegisterInitializer<DisposableCommand>(command =>
            {
                lifestyle.RegisterForDisposal(container, command);
                initializerCalled = true;
            });

            // Act
            container.Verify(VerificationOption.VerifyOnly);

            // Arrange
            Assert.IsTrue(initializerCalled);
        }
        public void RegisterForDisposal_OutsideTheContextOfAHttpRequest_ThrowsExpectedException()
        {
            // Arrange
            ScopedLifestyle lifestyle = new WebRequestLifestyle();

            var validContainer = new Container();
            var validInstance = new DisposableCommand();

            // Act
            Action action = () => lifestyle.RegisterForDisposal(validContainer, validInstance);

            // Assert
            AssertThat.ThrowsWithExceptionMessageContains<InvalidOperationException>(
                "This method can only be called within the context of an active Web Request.", action);
        }
        public void RegisterForDisposal_WithNullAction_ThrowsExpectedException()
        {
            // Arrange
            ScopedLifestyle lifestyle = new WebRequestLifestyle();

            IDisposable invalidInstance = null;

            using (new HttpContextScope())
            {
                // Act
                Action action = () => lifestyle.RegisterForDisposal(new Container(), invalidInstance);

                // Assert
                AssertThat.ThrowsWithParamName<ArgumentNullException>("disposable", action);
            }
        }
        public void RegisterForDisposal_WithNullContainer_ThrowsExpectedException()
        {
            // Arrange
            ScopedLifestyle lifestyle = new WebRequestLifestyle();

            Container invalidContainer = null;

            using (new HttpContextScope())
            {
                // Act
                Action action = () => lifestyle.RegisterForDisposal(invalidContainer, new DisposableCommand());

                // Assert
                AssertThat.ThrowsWithParamName<ArgumentNullException>("container", action);
            }
        }