public async Task when_processing_the_command() { FakeCommandTypeCollection = new Mock <ICommandTypeCollection>(); FakeServiceProvider = new Mock <IServiceProvider>(); Subject = new CommandProcessor(FakeCommandTypeCollection.Object, FakeServiceProvider.Object); async Task should_invoke_the_correct_command_handler() { FakeCommand expectedCommand = null; var fakeCommandHandler = new FakeCommandHandler(x => expectedCommand = x); FakeServiceProvider.Setup(x => x.GetService(typeof(IEnumerable <ICommandHandler <FakeCommand> >))).Returns(new[] { fakeCommandHandler }); var command = new FakeCommand(); await Subject.ProcessAsync(command); command.Should().Be(expectedCommand); } void should_throw_exception_if_the_command_handler_is_not_found() { var command = new Mock <ICommand>().Object; Subject.Awaiting(x => x.ProcessAsync(command)).Should() .Throw <CommandProcessorException>() .WithMessage($"The command handler for '{command}' could not be found"); } void should_throw_exception_if_multiple_command_handlers_are_found() { var handlerType = typeof(ICommandHandler <FakeMultiCommand1>); var enumerableType = typeof(IEnumerable <ICommandHandler <FakeMultiCommand1> >); FakeServiceProvider.Setup(x => x.GetService(enumerableType)).Returns(new[] { new Mock <ICommandHandler <FakeMultiCommand1> >().Object, new Mock <ICommandHandler <FakeMultiCommand1> >().Object }); var command = new FakeMultiCommand1(); Subject.Awaiting(x => x.ProcessAsync(command)).Should() .Throw <CommandProcessorException>() .WithMessage($"Multiple command handlers for '{handlerType}' was found"); } }
public async Task when_processing_the_command() { FakeCommandTypeCollection = new Mock <ICommandTypeCollection>(); FakeServiceProvider = new Mock <IServiceProvider>(); Subject = new CommandProcessor(FakeCommandTypeCollection.Object, FakeServiceProvider.Object); async Task should_invoke_the_correct_command_handler() { FakeCommand expectedCommand = null; var fakeCommandHandler = new FakeCommandHandler(x => expectedCommand = x); FakeServiceProvider.Setup(x => x.GetService(typeof(ICommandHandler <FakeCommand>))).Returns(fakeCommandHandler); var command = new FakeCommand(); await Subject.ProcessAsync(command); command.Should().Be(expectedCommand); } async Task should_create_the_command_from_a_string() { var expectedCommandType = typeof(FakeCommand); var fakeCommandHandler = new Mock <ICommandHandler <FakeCommand> >(); FakeCommandTypeCollection.Setup(x => x.GetType(expectedCommandType.Name)).Returns(expectedCommandType); FakeServiceProvider.Setup(x => x.GetService(typeof(ICommandHandler <FakeCommand>))).Returns(fakeCommandHandler.Object); await Subject.ProcessAsync(expectedCommandType.Name, "{}"); fakeCommandHandler.Verify(x => x.HandleAsync(It.IsAny <FakeCommand>())); } void should_throw_exception_if_the_command_handler_is_not_found() { var command = new Mock <ICommand>().Object; Subject.Awaiting(async x => await x.ProcessAsync(command)).Should() .Throw <CommandProcessorException>() .WithMessage($"The command handler for '{command}' could not be found"); } void should_throw_exception_if_the_command_type_is_not_found() { var commandName = "NotFoundCommand"; var json = JObject.Parse("{}"); Subject.Awaiting(async x => await x.ProcessAsync(commandName, json)).Should() .Throw <CommandProcessorException>() .WithMessage("The command type 'NotFoundCommand' could not be found"); } void should_throw_exception_if_the_json_is_invalid() { var commandName = "FakeCommand"; FakeCommandTypeCollection.Setup(x => x.GetType(commandName)).Returns(typeof(FakeCommand)); Subject.Awaiting(async x => await x.ProcessAsync(commandName, (JObject)null)).Should() .Throw <CommandProcessorException>() .WithMessage("The json could not be converted to an object"); } }