Beispiel #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);
            }
        }
Beispiel #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
            }));
        }
Beispiel #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
            }));
        }
Beispiel #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));
        }
Beispiel #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);
        }
Beispiel #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);
        }
Beispiel #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));
        }
Beispiel #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));
        }
Beispiel #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));
        }