コード例 #1
0
        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);
            }
        }
コード例 #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);
        }
コード例 #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);
            }
        }
コード例 #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);
            }
        }
コード例 #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);
        }
コード例 #6
0
        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.");
        }
コード例 #7
0
        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);
            }
        }
コード例 #8
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);
        }
コード例 #9
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);
        }
コード例 #10
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);
            }
        }
コード例 #11
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);
            }
        }