Esempio n. 1
0
        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");
            }
        }
Esempio n. 2
0
 public Task <FakeResult> HandleAsync(FakeMultiResultCommand1 command)
 {
     throw new NotImplementedException();
 }