示例#1
0
        public void NotAlterTheExceptionStackTraceOnThrownExceptions()
        {
            // Arrange
            var ioc = new InversionOfControl();

            ioc.Register <ICommandMediator, CommandMediator>();
            ioc.Register <ICommandHandler <ExplodingTestCommand, UnitType>, ExplodingTestCommandHandler>();

            var mediator = new CommandMediator(ioc.Resolve);

            const string ExpectedExceptionMessage = "Kaboom!";

            try
            {
                mediator.Submit(new ExplodingTestCommand {
                    ExceptionMessage = ExpectedExceptionMessage
                });
            }
            catch (Exception ex)
            {
                var stackTrace = new StackTrace(ex);
                var topFrame   = stackTrace.GetFrame(0);
                Assert.AreEqual(typeof(ExplodingTestCommandHandler), topFrame.GetMethod().DeclaringType);
            }
        }
示例#2
0
        public void ThrowCommandHandlerNotFoundExceptionGivenNoHandlerIsRegisteredAndThrowExceptionIsTrue()
        {
            // Arrange
            var ioc = new InversionOfControl();

            ioc.Register <ICommandHandler <DebugWriteCommand>, DebugWriteCommandHandler>();

            var mediator = new CommandMediator(ioc.Resolve);

            const string StringToWrite = "This is a test!";

            // Act & Assert
            Assert.Throws <CommandHandlerNotFoundException>(() => mediator.Submit(new DebugWriteCommand {
                TextToWrite = StringToWrite
            }));
        }
示例#3
0
        public void ThrowExceptionForGivenCommandGivenCommandThrowsExceptionAndThrowExceptionsIsTrue()
        {
            // Arrange
            var ioc = new InversionOfControl();

            ioc.Register <ICommandMediator, CommandMediator>();
            ioc.Register <ICommandHandler <ExplodingTestCommand, UnitType>, ExplodingTestCommandHandler>();

            var mediator = new CommandMediator(ioc.Resolve);

            const string ExpectedExceptionMessage = "Kaboom!";

            // Act & Assert
            Assert.Throws <Exception>(() => mediator.Submit(new ExplodingTestCommand {
                ExceptionMessage = ExpectedExceptionMessage
            }));
        }
示例#4
0
        public void ReturnResponseWithExecutionResultSetToFailedGivenNoHandlerIsRegisteredAndThrowExceptionIsFalse()
        {
            // Arrange
            var ioc = new InversionOfControl();

            ioc.Register <ICommandHandler <DebugWriteCommand>, DebugWriteCommandHandler>();

            var mediator = new CommandMediator(ioc.Resolve, false);

            const string StringToWrite = "This is a test!";

            // Act
            var response = mediator.Submit(new DebugWriteCommand {
                TextToWrite = StringToWrite
            });

            // Assert
            Assert.That(response.CommandExecutionResult.Equals(CommandExecutionResult.Failed));
        }
示例#5
0
        public void ReturnResponseWithExecutionTime()
        {
            // Arrange
            var ioc = new InversionOfControl();

            ioc.Register <ICommandMediator, CommandMediator>();
            ioc.Register <ICommandHandler <DebugWriteCommand, UnitType>, DebugWriteCommandHandler>();

            var mediator = new CommandMediator(ioc.Resolve);

            const string StringToWrite = "This is a test!";

            // Act
            var response = mediator.Submit(new DebugWriteCommand {
                TextToWrite = StringToWrite
            });

            // Assert
            Assert.GreaterOrEqual(response.ExecutionTimeInMilliseconds, 50);
        }
示例#6
0
        public void ReturnResponseForCommandGivenHandlerIsRegistered()
        {
            // Arrange
            var ioc = new InversionOfControl();

            ioc.Register <ICommandMediator, CommandMediator>();
            ioc.Register <ICommandHandler <DebugWriteCommand, UnitType>, DebugWriteCommandHandler>();

            var mediator = new CommandMediator(ioc.Resolve);

            const string StringToWrite = "This is a test!";

            // Act
            var response = mediator.Submit(new DebugWriteCommand {
                TextToWrite = StringToWrite
            });

            // Assert
            Assert.NotNull(response);
        }
示例#7
0
        public void ReturnResponseWithExecutionResultSetToFailedGivenCommandThrowsException()
        {
            // Arrange
            var ioc = new InversionOfControl();

            ioc.Register <ICommandMediator, CommandMediator>();
            ioc.Register <ICommandHandler <ExplodingTestCommand, UnitType>, ExplodingTestCommandHandler>();

            var mediator = new CommandMediator(ioc.Resolve, false);

            const string ExpectedExceptionMessage = "Kaboom!";

            // Act & Assert
            var response = mediator.Submit(new ExplodingTestCommand {
                ExceptionMessage = ExpectedExceptionMessage
            });

            // Assert
            Assert.That(response.CommandExecutionResult.Equals(CommandExecutionResult.Failed));
        }
示例#8
0
        public void ReturnResponseWithExceptionForCommandGivenCommandThrowsExceptionAndThrowExceptionIsFalse()
        {
            // Arrange
            var ioc = new InversionOfControl();

            ioc.Register <ICommandMediator, CommandMediator>();
            ioc.Register <ICommandHandler <ExplodingTestCommand, UnitType>, ExplodingTestCommandHandler>();

            var mediator = new CommandMediator(ioc.Resolve, false);

            const string ExpectedExceptionMessage = "Kaboom!";

            // Act & Assert
            var response = mediator.Submit(new ExplodingTestCommand {
                ExceptionMessage = ExpectedExceptionMessage
            });

            // Assert
            Assert.NotNull(response.Exception);
            Assert.That(response.Exception.Message.Equals(ExpectedExceptionMessage));
        }
示例#9
0
        public void ReturnResponseWithValidationResultsGivenCommandFailsValidation()
        {
            // Arrange
            var ioc = new InversionOfControl();

            ioc.Register <ICommandMediator, CommandMediator>();
            ioc.Register <ICommandHandler <DebugWriteWithValidationCommand, UnitType>, DebugWriteWithValidationCommandHandler>();

            var mediator = new CommandMediator(ioc.Resolve);

            const string ExpectedValidationMessage = "You must supply a value for TextToWrite!";

            // Act & Assert
            var response = mediator.Submit(new DebugWriteWithValidationCommand()
            {
                TextToWrite = "Blarg"
            });

            // Assert
            Assert.That(response.CommandExecutionResult.Equals(CommandExecutionResult.Failed));
            Assert.That(response.ValidationResults.Any().IsTrue());
            Assert.That(response.ValidationResults.First().ErrorMessage.Equals(ExpectedValidationMessage));
        }