public void LifetimeScopeDispose_WithWhenScopeEndsRegistration_CallsTheRegisteredAction()
        {
            // Arrange
            int actionCallCount = 0;

            var container = new Container();

            var lifestyle = new ThreadScopedLifestyle();

            container.Register <DisposableCommand, DisposableCommand>(lifestyle);

            container.RegisterInitializer <DisposableCommand>(command =>
            {
                lifestyle.WhenScopeEnds(container, () => { actionCallCount++; });
            });

            using (ThreadScopedLifestyle.BeginScope(container))
            {
                container.GetInstance <DisposableCommand>();

                // Act
            }

            // Assert
            Assert.AreEqual(1, actionCallCount, "Delegate is expected to be called exactly once.");
        }
        public void WhenScopeEnds_NullActionArgument_ThrowsException()
        {
            // Arrange
            var lifestyle = new ThreadScopedLifestyle();

            // Act
            Action action = () => lifestyle.WhenScopeEnds(new Container(), null);

            // Assert
            AssertThat.Throws <ArgumentNullException>(action);
        }
        public void Verify_WithWhenScopeEndsRegistration_Succeeds()
        {
            // Arrange
            var container = new Container();

            var lifestyle = new ThreadScopedLifestyle();

            container.Register <ICommand, DisposableCommand>(lifestyle);

            container.RegisterInitializer <DisposableCommand>(command =>
            {
                lifestyle.WhenScopeEnds(container, () => { });
            });

            // Act
            container.Verify();
        }
        public void WhenScopeEnds_CalledOutOfTheContextOfALifetimeScope_ThrowsException()
        {
            // Arrange
            var lifestyle = new ThreadScopedLifestyle();

            var container = new Container();

            container.Register <ConcreteCommand>(lifestyle);

            try
            {
                // Act
                lifestyle.WhenScopeEnds(container, () => { });

                // Assert
                Assert.Fail("Exception expected.");
            }
            catch (InvalidOperationException ex)
            {
                Assert.IsTrue(ex.Message.Contains(
                                  "This method can only be called within the context of an active (Thread Scoped) scope."),
                              "Actual: " + ex.Message);
            }
        }