예제 #1
0
        public async Task Handle_sends_message_to_all_handlers_even_if_some_handler_fails()
        {
            var handler1 = Mock.Of <IMessageHandler>();
            var handler2 = Mock.Of <IMessageHandler>();
            var sut      = new CompositeMessageHandler(handler1, handler2);
            var message  = new object();
            var envelope = new Envelope(message);

            Mock.Get(handler1)
            .Setup(x => x.Handle(envelope, CancellationToken.None))
            .Throws <InvalidOperationException>();

            try
            {
                await sut.Handle(envelope, CancellationToken.None);
            }
            catch
            {
            }

            Mock.Get(handler1).Verify(
                x => x.Handle(envelope, CancellationToken.None), Times.Once());
            Mock.Get(handler2).Verify(
                x => x.Handle(envelope, CancellationToken.None), Times.Once());
        }
예제 #2
0
        public void Handle_throws_aggregate_exception_if_handler_fails()
        {
            // Arrange
            var handler1 = Mock.Of <IMessageHandler>();
            var handler2 = Mock.Of <IMessageHandler>();

            var sut = new CompositeMessageHandler(handler1, handler2);

            var message  = new object();
            var envelope = new Envelope(message);

            var exception1 = new InvalidOperationException();

            Mock.Get(handler1)
            .Setup(x => x.Handle(envelope, CancellationToken.None))
            .Throws(exception1);

            var exception2 = new InvalidOperationException();

            Mock.Get(handler2)
            .Setup(x => x.Handle(envelope, CancellationToken.None))
            .Throws(exception2);

            // Act
            Func <Task> action = () => sut.Handle(envelope, CancellationToken.None);

            // Arrange
            action.ShouldThrow <AggregateException>()
            .Which.InnerExceptions.Should().ContainSingle()
            .Which.Should().BeOfType <AggregateException>()
            .Which.InnerExceptions
            .ShouldAllBeEquivalentTo(new[] { exception1, exception2 });
        }
        public void constructor_sets_Handles_correctly()
        {
            IMessageHandler handler1 = Mock.Of <IMessageHandler>();
            IMessageHandler handler2 = Mock.Of <IMessageHandler>();
            IMessageHandler handler3 = Mock.Of <IMessageHandler>();

            var sut = new CompositeMessageHandler(handler1, handler2, handler3);

            sut.Handlers.Should().Equal(handler1, handler2, handler3);
        }
        public void Accepts_returns_false_if_no_handler_accepts_message()
        {
            Envelope        envelope = new Fixture().Create <Envelope>();
            IMessageHandler handler1 = Mock.Of <IMessageHandler>(x => x.Accepts(envelope) == false);
            IMessageHandler handler2 = Mock.Of <IMessageHandler>(x => x.Accepts(envelope) == false);
            IMessageHandler handler3 = Mock.Of <IMessageHandler>(x => x.Accepts(envelope) == false);
            var             sut      = new CompositeMessageHandler(handler1, handler2, handler3);

            bool actual = sut.Accepts(envelope);

            actual.Should().BeFalse();
        }
        public void Accepts_returns_true_if_some_handlers_accept_message(bool accepts1, bool accepts2, bool accepts3)
        {
            Envelope        envelope = new Fixture().Create <Envelope>();
            IMessageHandler handler1 = Mock.Of <IMessageHandler>(x => x.Accepts(envelope) == accepts1);
            IMessageHandler handler2 = Mock.Of <IMessageHandler>(x => x.Accepts(envelope) == accepts2);
            IMessageHandler handler3 = Mock.Of <IMessageHandler>(x => x.Accepts(envelope) == accepts3);
            var             sut      = new CompositeMessageHandler(handler1, handler2, handler3);

            bool actual = sut.Accepts(envelope);

            actual.Should().BeTrue();
        }
예제 #6
0
        public async Task Handle_sends_message_to_all_handlers()
        {
            var handler1 = Mock.Of <IMessageHandler>();
            var handler2 = Mock.Of <IMessageHandler>();
            var sut      = new CompositeMessageHandler(handler1, handler2);
            var message  = new object();
            var envelope = new Envelope(message);

            await sut.Handle(envelope, CancellationToken.None);

            Mock.Get(handler1).Verify(
                x => x.Handle(envelope, CancellationToken.None), Times.Once());
            Mock.Get(handler2).Verify(
                x => x.Handle(envelope, CancellationToken.None), Times.Once());
        }
예제 #7
0
        public void sut_implements_IMessageHandler()
        {
            var sut = new CompositeMessageHandler();

            sut.Should().BeAssignableTo <IMessageHandler>();
        }