Пример #1
0
        public void EventSubscription_ctor_subscribe_different_type_event_hendler()
        {
            // Arrange
            var instanceMock = new Mock <IDummyObserver>();
            var source       = new DummyEventSource();

            // Act
            var subscription = new EventSubscription(
                source, nameof(source.InstanceHandler1), new Action <object, EventArgs>(instanceMock.Object.EventHandler));

            // Assert
            var args = new EventArgs();

            source.InvokeInstanceHandler1(args);
            instanceMock.Verify(e => e.EventHandler(source, args), Times.Once);
        }
        public void AddEventSubscription_adds_instance_handler_delegate_to_the_DisposableContainer()
        {
            var container    = new DisposableContainer();
            var instanceMock = new Mock <IDummyObserver>();
            var source       = new DummyEventSource();
            var args         = new EventArgs();

            container.AddEventSubscription <object, EventArgs>(
                source,
                nameof(source.InstanceHandler1),
                instanceMock.Object.EventHandler);
            source.InvokeInstanceHandler1(args);
            container.Dispose();
            source.InvokeInstanceHandler1(args);

            instanceMock.Verify(d => d.EventHandler(It.IsAny <object>(), It.IsAny <EventArgs>()), Times.Once);
        }
Пример #3
0
        public void EventSubscription_Dispose_unsubscribe_same_type_event()
        {
            // Arrange
            var instanceMock = new Mock <IDummyObserver>();
            var source       = new DummyEventSource();
            var subscription = new EventSubscription(
                source, nameof(source.InstanceHandler1), new EventHandler(instanceMock.Object.EventHandler));

            // Act
            subscription.Dispose();

            // Assert
            var args = new EventArgs();

            source.InvokeInstanceHandler1(args);
            instanceMock.Verify(e => e.EventHandler(It.IsAny <object>(), It.IsAny <EventArgs>()), Times.Never);
        }
Пример #4
0
        public void EventSubscription_ctor_subscribe_multi_same_type_delegate()
        {
            // Arrange
            var instanceMock = new Mock <IDummyObserver>();
            var source       = new DummyEventSource();

            // Act
            var subscription = new EventSubscription(
                source, nameof(source.InstanceHandler1), Delegate.Combine(
                    new EventHandler(instanceMock.Object.EventHandler),
                    new EventHandler(instanceMock.Object.EventHandler)
                    ));

            // Assert
            var args = new EventArgs();

            source.InvokeInstanceHandler1(args);
            instanceMock.Verify(e => e.EventHandler(source, args), Times.Exactly(2));
        }