public async Task Should_not_set_handled_if_no_handler_registered()
        {
            var sut = new InMemoryCommandBus(new ICommandMiddleware[0]);
            var ctx = await sut.PublishAsync(command);

            Assert.False(ctx.IsCompleted);
        }
        public async Task Should_throw_and_call_all_handlers_if_first_handler_fails()
        {
            var handler = new ThrowHandledHandler();

            var sut = new InMemoryCommandBus(new ICommandMiddleware[] { handler });

            await Assert.ThrowsAsync <InvalidOperationException>(async() => await sut.PublishAsync(command));

            Assert.Equal(command, handler.LastCommand);
        }
        public async Task Should_set_succeeded_if_handler_marks_completed()
        {
            var handler = new HandledHandler();

            var sut = new InMemoryCommandBus(new ICommandMiddleware[] { handler });
            var ctx = await sut.PublishAsync(command);

            Assert.Equal(command, handler.LastCommand);
            Assert.True(ctx.IsCompleted);
        }
        public async Task Should_not_set_succeeded_if_handler_returns_false()
        {
            var handler = new NonHandledHandler();

            var sut = new InMemoryCommandBus(new ICommandMiddleware[] { handler });
            var ctx = await sut.PublishAsync(command);

            Assert.Equal(command, handler.LastCommand);
            Assert.False(ctx.IsCompleted);
        }
        public async Task Should_set_succeeded_if_handler_returns_true()
        {
            var handler = new HandledHandler();

            var sut = new InMemoryCommandBus(new ICommandHandler[] { handler });
            var ctx = await sut.PublishAsync(command);

            Assert.Equal(command, handler.LastCommand);
            Assert.True(ctx.IsSucceeded);
            Assert.True(ctx.IsHandled);
            Assert.Null(ctx.Exception);
        }
        public async Task Should_throw_and_call_all_handlers_if_first_handler_fails()
        {
            var handler1 = new ThrowHandledHandler();
            var handler2 = new AfterThrowHandler();

            var sut = new InMemoryCommandBus(new ICommandHandler[] { handler1, handler2 });

            await Assert.ThrowsAsync <InvalidOperationException>(async() => await sut.PublishAsync(command));

            Assert.Equal(command, handler1.LastCommand);
            Assert.IsType <InvalidOperationException>(handler2.LastException);
        }