public void Dispatch_BuildGenericCommandHandlerAndGetFromContainer_ShouldHandleCommand(
            [Frozen] Mock<IContainer> container,
            [Frozen] Mock<ICommandHandler<FakeCommand>> commandHandler,
            FakeCommand command,
            CommandDispatcher dispatcher)
        {
            // Arrange

            var genericTypeName = typeof (ICommandHandler<FakeCommand>).FullName;

            container.Setup(c => c.GetInstance(It.Is<Type>(t => t.FullName == genericTypeName)))
                     .Returns(commandHandler.Object);

            // Act

            dispatcher.Dispatch(command);

            // Assert

            commandHandler.Verify(t => t.Handle(command), Times.Once);
        }
        public void Dispatch_CommandThrowsCommandException_ShouldHandleExceptionAndRethrow(
            [Frozen] Mock<IContainer> container,
            [Frozen] Mock<ICommandHandler<ICommand>> commandHandler,
            FakeCommand command,
            CommandDispatcher dispatcher)
        {
            // Arrange

            var commandException = new CommandException("Exception");

            commandHandler.Setup(h => h.Handle(It.IsAny<FakeCommand>())).Throws(commandException);

            var genericTypeName = typeof(ICommandHandler<FakeCommand>).FullName;

            container.Setup(c => c.GetInstance(It.Is<Type>(t => t.FullName == genericTypeName)))
                     .Returns(commandHandler.Object);

            var commandExceptionGenericTypeName = typeof(ICommandHandler<CommandException>).FullName;

            container.Setup(c => c.GetInstance(It.Is<Type>(t => t.FullName == commandExceptionGenericTypeName)))
                     .Returns(commandHandler.Object);

            // Act

            Assert.Throws<CommandException>(() => dispatcher.Dispatch(command));

            // Assert

            commandHandler.Verify(t => t.Handle(command), Times.Once);

            commandHandler.Verify(h => h.Handle(commandException), Times.Once);
        }
 public void Dispatch_NullCommand_ThrowsArgumentNullException(CommandDispatcher dispatcher)
 {
     Assert.Throws<ArgumentNullException>(() => dispatcher.Dispatch(null));
 }
        public void Dispatch_CommandThrowsActivationException_ShouldThrowCommandHandlerNotFoundException(
            [Frozen] Mock<IContainer> container,
            [Frozen] Mock<ICommandHandler<ICommand>> commandHandler,
            FakeCommand command,
            CommandDispatcher dispatcher)
        {
            // Arrange

            commandHandler.Setup(h => h.Handle(It.IsAny<FakeCommand>())).Throws<Exception>();

            container.Setup(c => c.GetInstance(It.IsAny<Type>())).Throws<Exception>();

            container.Setup(c => c.IsActivationException(It.IsAny<Exception>())).Returns(true);

            // Act and assert

            Assert.Throws<CommandHandlerNotFoundException>(() => dispatcher.Dispatch(command));
        }