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() { var fakeCommandHandler = new FakeCommandHandler(x => Expected = x); FakeServiceProvider.Setup(x => x.GetService(typeof(ICommandHandler <FakeCommand>))).Returns(fakeCommandHandler); var command = new FakeCommand(); await Subject.ProcessAsync(command); Expected.Should().Be(command); } 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"); } }
public async Task when_processing_the_command_with_result() { FakeCommandTypeCollection = new Mock <ICommandTypeCollection>(); FakeServiceProvider = new Mock <IServiceProvider>(); Subject = new CommandProcessor(FakeCommandTypeCollection.Object, FakeServiceProvider.Object); async Task should_invoke_the_correct_command_handler_and_return_a_result() { FakeResultCommand expectedCommand = null; var expectedResult = new FakeResult(); var fakeCommandHandler = new FakeResultCommandHandler(x => { expectedCommand = x; return(expectedResult); }); FakeServiceProvider.Setup(x => x.GetService(typeof(IEnumerable <ICommandHandler <FakeResultCommand, FakeResult> >))).Returns(new[] { fakeCommandHandler }); var command = new FakeResultCommand(); var result = await Subject.ProcessWithResultAsync(command); command.Should().Be(expectedCommand); result.Should().Be(expectedResult); } void should_throw_exception_if_the_command_handler_is_not_found() { var command = new Mock <ICommand <object> >().Object; Subject.Awaiting(x => x.ProcessWithResultAsync(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 <FakeMultiResultCommand1, FakeResult>); var enumerableType = typeof(IEnumerable <ICommandHandler <FakeMultiResultCommand1, FakeResult> >); FakeServiceProvider.Setup(x => x.GetService(enumerableType)).Returns(new[] { new Mock <ICommandHandler <FakeMultiResultCommand1, FakeResult> >().Object, new Mock <ICommandHandler <FakeMultiResultCommand1, FakeResult> >().Object }); var command = new FakeMultiResultCommand1(); Subject.Awaiting(x => x.ProcessWithResultAsync(command)).Should() .Throw <CommandProcessorException>() .WithMessage($"Multiple command handlers for '{handlerType}' was found"); } }
public async Task when_processing_the_command_with_result() { FakeCommandTypeCollection = new Mock <ICommandTypeCollection>(); FakeServiceProvider = new Mock <IServiceProvider>(); Subject = new CommandProcessor(FakeCommandTypeCollection.Object, FakeServiceProvider.Object); async Task should_invoke_the_correct_command_handler_and_return_a_result() { FakeResultCommand expectedCommand = null; var expectedResult = new FakeResult(); var fakeCommandHandler = new FakeResultCommandHandler(x => { expectedCommand = x; return(expectedResult); }); FakeServiceProvider.Setup(x => x.GetService(typeof(ICommandHandler <FakeResultCommand, FakeResult>))).Returns(fakeCommandHandler); var command = new FakeResultCommand(); var result = await Subject.ProcessWithResultAsync(command); command.Should().Be(expectedCommand); result.Should().Be(expectedResult); } async Task should_create_the_command_from_a_string() { var expectedCommandType = typeof(FakeResultCommand); var fakeCommandHandler = new Mock <ICommandHandler <FakeResultCommand, FakeResult> >(); FakeCommandTypeCollection.Setup(x => x.GetType(expectedCommandType.Name)).Returns(expectedCommandType); FakeServiceProvider.Setup(x => x.GetService(typeof(ICommandHandler <FakeResultCommand, FakeResult>))).Returns(fakeCommandHandler.Object); await Subject.ProcessWithResultAsync <FakeResult>(expectedCommandType.Name, "{}"); fakeCommandHandler.Verify(x => x.HandleAsync(It.IsAny <FakeResultCommand>())); } void should_throw_exception_if_the_command_handler_is_not_found() { var command = new Mock <ICommand <object> >().Object; Subject.Awaiting(async x => await x.ProcessWithResultAsync(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.ProcessWithResultAsync <object>(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.ProcessWithResultAsync <object>(commandName, (JObject)null)).Should() .Throw <CommandProcessorException>() .WithMessage("The json could not be converted to an object"); } }