Example #1
0
        public void WhenGettingDependencyScopeFromDeepestRequestThenDelegatesToDeepestDependencyResolver()
        {
            // Arrange
            Mock <ICommand> command      = new Mock <ICommand>();
            HandlerRequest  request      = new CommandHandlerRequest(this.defaultConfig, command.Object);
            HandlerRequest  innerRequest = new CommandHandlerRequest(this.defaultConfig, command.Object, request);

            Mock <IDependencyResolver> resolver = new Mock <IDependencyResolver>(MockBehavior.Strict);

            resolver
            .Setup(r => r.BeginScope())
            .Returns(CreateScopeMock);
            resolver
            .Setup(r => r.Dispose());
            this.defaultConfig.DependencyResolver = resolver.Object;

            // Act
            var scope1 = request.GetDependencyScope();
            var scope2 = request.GetDependencyScope(false);
            var scope3 = innerRequest.GetDependencyScope();
            var scope4 = innerRequest.GetDependencyScope(false);

            // Assert
            Assert.NotNull(scope1);
            Assert.NotNull(scope2);
            Assert.NotNull(scope3);
            Assert.NotNull(scope4);
            Assert.Same(scope1, scope2);
            Assert.Same(scope1, scope3);
            Assert.NotSame(scope1, scope4);
            resolver.Verify(r => r.BeginScope(), Times.Exactly(2));
        }
Example #2
0
        public void WhenGettingDependencyScopeThenDelegatesToDependencyResolver()
        {
            // Arrange
            Mock <ICommand>         command = new Mock <ICommand>();
            HandlerRequest          request = new CommandHandlerRequest(this.defaultConfig, command.Object);
            Mock <IDependencyScope> scope   = new Mock <IDependencyScope>(MockBehavior.Strict);

            scope.Setup(s => s.Dispose());

            Mock <IDependencyResolver> resolver = new Mock <IDependencyResolver>(MockBehavior.Strict);

            resolver
            .Setup(r => r.BeginScope())
            .Returns(scope.Object);
            resolver
            .Setup(r => r.Dispose());
            this.defaultConfig.DependencyResolver = resolver.Object;

            // Act
            var scope1 = request.GetDependencyScope();
            var scope2 = request.GetDependencyScope();

            // Assert
            Assert.NotNull(scope1);
            Assert.Same(scope1, scope2);
            resolver.Verify(r => r.BeginScope(), Times.Once());
        }
Example #3
0
        public void WhenDisposingScopeFromDeepestRequestThenScopesAreDisposed()
        {
            // Arrange
            Mock <ICommand> command      = new Mock <ICommand>();
            HandlerRequest  request      = new CommandHandlerRequest(this.defaultConfig, command.Object);
            HandlerRequest  innerRequest = new CommandHandlerRequest(this.defaultConfig, command.Object, request);

            Mock <IDependencyResolver> resolver = new Mock <IDependencyResolver>(MockBehavior.Strict);
            int disposedCount = 0;

            resolver
            .Setup(r => r.BeginScope())
            .Returns(() => CreateVerifiableScopeMock(() => disposedCount++));
            resolver
            .Setup(r => r.Dispose());
            this.defaultConfig.DependencyResolver = resolver.Object;

            request.GetDependencyScope();
            innerRequest.GetDependencyScope(false);

            // Act & Assert
            Assert.Equal(0, disposedCount);
            innerRequest.Dispose();
            Assert.Equal(1, disposedCount);
            request.Dispose();
            Assert.Equal(2, disposedCount);
        }
Example #4
0
        public void WhenDisposingThenScopeIsDisposed()
        {
            // Arrange
            Mock <ICommand>         command = new Mock <ICommand>();
            HandlerRequest          request = new CommandHandlerRequest(this.defaultConfig, command.Object);
            Mock <IDependencyScope> scope   = new Mock <IDependencyScope>(MockBehavior.Strict);

            scope.Setup(s => s.Dispose());

            Mock <IDependencyResolver> resolver = new Mock <IDependencyResolver>(MockBehavior.Strict);

            resolver
            .Setup(r => r.BeginScope())
            .Returns(scope.Object);
            resolver
            .Setup(r => r.Dispose());
            this.defaultConfig.DependencyResolver = resolver.Object;
            request.GetDependencyScope();

            // Act
            request.Dispose();

            // Assert
            scope.Verify(s => s.Dispose(), Times.Once());
        }