public async Task Setup()
        {
            cancellationTokenSource = new CancellationTokenSource();

            pipeline = new IncomingPipeline(
                mockMessageSource.Object,
                new[] { mockBehaviour.Object },
                mockServiceScopeFactory.Object,
                mockHandlerInvoker.Object,
                NullLogger <IncomingPipeline> .Instance,
                retryOptions);

            mockBehaviour
            .Setup(
                m => m.Process(
                    It.Is <IncomingMessage>(message => ((string)message.Body) == "throw this"),
                    It.IsAny <Context>(),
                    It.IsAny <IncomingPipelineAction>()))
            .Throws(new InvalidOperationException());

            messageCausingException = IncomingMessageBuilder.BuildWithBody("throw this");
            normalMessage           = IncomingMessageBuilder.BuildDefault();

            await pipeline
            .Initialise(CancellationToken.None)
            .ConfigureAwait(false);
        }
        public async Task InvokeHandlersForMessage()
        {
            using (var serviceScope = serviceProvider.CreateScope())
            {
                var message = IncomingMessageBuilder.BuildWithBody(new ExampleEvent());

                await behaviour
                .Process(
                    message,
                    new Context(serviceScope))
                .ConfigureAwait(false);

                Assert.IsNotNull(mockHandler.CalledWith);
                Assert.AreSame(message.Body, mockHandler.CalledWith);
            }
        }
        public async Task NotWrapExceptionsInHandlersWithAnInvocationException()
        {
            using (var serviceScope = serviceProvider.CreateScope())
            {
                var message = IncomingMessageBuilder.BuildWithBody(new ExampleEvent());

                mockHandler.ThrowException = true;

                var exception = await Assert
                                .ThrowsExceptionAsync <InvalidOperationException>(
                    () => behaviour
                    .Process(
                        message,
                        new Context(serviceScope)))
                                .ConfigureAwait(false);

                Assert.AreEqual("Was told to throw.", exception.Message);
            }
        }