Пример #1
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);
        }
Пример #2
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());
        }
Пример #3
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);
        }
Пример #4
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);
        }
Пример #5
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());
        }
Пример #6
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);
        }
Пример #7
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);
        }