コード例 #1
0
        public void Handler_ContextShouldNot_BeChanged()
        {
            //Arrange
            var handler = new Handler();
            var context = new Context()
            {
                Data = 0
            };

            //Act
            //Assert
            handler.Subscribe <ContextfulEvent, Context>((ctx) => {
                Assert.AreEqual(ctx.Data, 0);
                ctx.Data += 1;
            });
            handler.Subscribe <ContextfulEvent, Context>((ctx) => {
                Assert.AreEqual(ctx.Data, 0);
                ctx.Data += 1;
            });
            handler.Subscribe <ContextfulEvent, Context>((ctx) => {
                Assert.AreEqual(ctx.Data, 0);
                ctx.Data += 1;
            });

            handler.Dispatch <ContextfulEvent, Context>(context);

            Assert.AreEqual(context.Data, 0);
        }
コード例 #2
0
        public void Handler_ShouldThrow_WhenSubscribing_IfSubscriptionIsNull()
        {
            //Arrange
            var handler = new Handler();
            ISubscription <ContextlessEvent> subscription = null;

            //Act
            //Assert
            Assert.ThrowsException <ArgumentException>(() => handler.Subscribe(subscription));
        }
コード例 #3
0
        public void Handler_ShouldThrowException_WhenUnsubscribing_WhenSubscriptionIsNull()
        {
            //Arrange
            var handler = new Handler();
            ContextfulSubscription subscription = null;

            //Act
            //Assert
            Assert.ThrowsException <ArgumentException>(() => handler.Subscribe(subscription));
        }
コード例 #4
0
        public void Handler_ShouldThrowException_WhenUnsubscribing_WhenEventIsNotSubscribed()
        {
            //Arrange
            var handler      = new Handler();
            var subscription = new ContextfulSubscription();

            //Act
            //Assert
            Assert.ThrowsException <ArgumentException>(() => handler.Unsubscribe(subscription));
        }
コード例 #5
0
        public void Handler_ShouldThrowException_WhenSubscribedTwice()
        {
            //Arrange
            var handler      = new Handler();
            var subscription = new ContextlessSubscription();

            handler.Subscribe(subscription);

            //Act
            //Assert
            Assert.ThrowsException <ArgumentException>(() => handler.Subscribe(subscription));
        }
コード例 #6
0
        public void Handler_ShouldNotify_ContexlessSubscribe()
        {
            //Arrange
            var handler          = new Handler();
            var subscriptionMock = new Mock <ContextlessSubscription>();

            subscriptionMock.Setup(m => m.Handle());
            handler.Subscribe(subscriptionMock.Object);

            //Act
            handler.Dispatch <ContextlessEvent>();

            //Assert
            subscriptionMock.Verify(x => x.Handle(), Times.Once());
        }
コード例 #7
0
        public void Handler_ShouldThrow_WhenSubscriptionForDualEvent_IsNotImplementedProperly()
        {
            //Arrange
            var handler = new Handler();
            var context = new DualContext();
            var subs    = new InvalidDualSubscription();

            handler.Subscribe(subs);

            //Act
            handler.Dispatch <DualEvent, DualContext>(context);

            //Assert
            Assert.ThrowsException <InvalidOperationException>(() => handler.Dispatch <DualEvent>());
        }
コード例 #8
0
        public void Handler_ShouldNotify_ContexlessLambda()
        {
            //Arrange
            var subscriptionCalled = false;
            var handler            = new Handler();

            handler.Subscribe <ContextlessEvent>(() => {
                subscriptionCalled = true;
            });

            //Act
            handler.Dispatch <ContextlessEvent>();

            //Assert
            Assert.IsTrue(subscriptionCalled);
        }
コード例 #9
0
        public void Handler_ShouldDispatchAll_WhenSubscriptionForDualEvent_IsImplementedProperly()
        {
            //Arrange
            var subscriptionsHandled = 0;
            var handler = new Handler();
            var context = new DualContext();
            var subs    = new ValidDualSubscription(() => subscriptionsHandled++);

            handler.Subscribe <DualEvent>(subs);

            //Act
            handler.Dispatch <DualEvent, DualContext>(context);
            handler.Dispatch <DualEvent>();

            //Assert
            Assert.AreEqual(2, subscriptionsHandled);
        }
コード例 #10
0
        public void Handler_ShouldNotNotify_WhenSubscriptionRemoved()
        {
            //Arrange
            var subscriptionCalled = false;
            var handler            = new Handler();
            var subscription       = handler.Subscribe <ContextlessEvent>(() => {
                subscriptionCalled = true;
            });

            handler.Unsubscribe(subscription);

            //Act
            handler.Dispatch <ContextlessEvent>();

            //Assert
            Assert.IsFalse(subscriptionCalled);
        }
コード例 #11
0
        public void Handler_ShouldNotify_ContexfulSubscribe()
        {
            //Arrange
            var handler          = new Handler();
            var subscriptionMock = new Mock <ContextfulSubscription>();
            var context          = new Context()
            {
                Data = 42
            };

            subscriptionMock.Setup(m => m.Handle(It.IsAny <Context>()));
            handler.Subscribe(subscriptionMock.Object);

            //Act
            handler.Dispatch <ContextfulEvent, Context>(context);

            //Assert
            subscriptionMock.Verify(x => x.Handle(context), Times.Once());
        }
コード例 #12
0
        public void Handler_ShouldNotify_ContexfulLambda()
        {
            //Arrange
            var subscriptionCalled = false;
            var handler            = new Handler();
            var context            = new Context()
            {
                Data = 42
            };

            handler.Subscribe <ContextfulEvent, Context>((ctx) => {
                subscriptionCalled = true;
            });

            //Act
            handler.Dispatch <ContextfulEvent, Context>(context);

            //Assert
            Assert.IsTrue(subscriptionCalled);
        }
コード例 #13
0
        public void Handler_ShouldNotify_AllSubscriptions()
        {
            //Arrange
            var subscriptionsHandled = 0;
            var handler = new Handler();

            handler.Subscribe <ContextlessEvent>(() => {
                subscriptionsHandled++;
            });
            handler.Subscribe <ContextlessEvent>(() => {
                subscriptionsHandled++;
            });
            handler.Subscribe <ContextlessEvent>(() => {
                subscriptionsHandled++;
            });

            //Act
            handler.Dispatch <ContextlessEvent>();

            //Assert
            Assert.AreEqual(subscriptionsHandled, 3);
        }
コード例 #14
0
        public void Handler_ShouldNotThrow_WhenSubscriptionForDualEvent_IsImplementedProperly()
        {
            //Arrange
            var timesDispatched = 0;
            var handler         = new Handler();
            var context         = new DualContext();
            var subs            = new ValidDualSubscription(() => { timesDispatched++; });

            handler.Subscribe <DualEvent>(subs);

            //Act
            //Assert
            try
            {
                handler.Dispatch <DualEvent, DualContext>(context);
                handler.Dispatch <DualEvent>();
            }
            catch (Exception e)
            {
                Assert.Fail($"Expected no exception, but got {e.Message}");
            }
            Assert.AreEqual(timesDispatched, 2);
        }