private void CreateAndRegisterSubscriber(IEventAggregator eventAggregator)
        {
            var sub = new TestEventSubscriber <CustomerAddedEvent>();

            eventAggregator.RegisterSubscriber(sub);

            _weakReference = new WeakReference(sub);
        }
        public void ShouldSubscribeToEvent()
        {
            var eventAggregator = new EventAggregator();

            var eventSubscriber = new TestEventSubscriber <CustomerAddedEvent>();

            eventAggregator.RegisterSubscriber(eventSubscriber);

            eventAggregator.Publish(new CustomerAddedEvent(5));

            Assert.Single(eventSubscriber.ReceivedEvents);
            Assert.Equal(5, eventSubscriber.ReceivedEvents[0].CustomerId);
        }
        public void ShouldReceiveEventThreeTimes()
        {
            var eventAggregator = new EventAggregator();

            var eventSubscriber = new TestEventSubscriber <CustomerAddedEvent>();

            eventAggregator.RegisterSubscriber(eventSubscriber);

            eventAggregator.Publish(new CustomerAddedEvent(5));
            eventAggregator.Publish(new CustomerAddedEvent(6));
            eventAggregator.Publish(new CustomerAddedEvent(7));

            Assert.Equal(3, eventSubscriber.ReceivedEvents.Count);
            Assert.Equal(5, eventSubscriber.ReceivedEvents[0].CustomerId);
            Assert.Equal(6, eventSubscriber.ReceivedEvents[1].CustomerId);
            Assert.Equal(7, eventSubscriber.ReceivedEvents[2].CustomerId);
        }
        public void ShouldRemoveDeadSubscribersAfterUnregisterASubscriber(int expectedNumberOfSubscribers, bool garbageCollect)
        {
            var eventAggregator = new EventAggregator();

            CreateAndRegisterSubscriber(eventAggregator);
            var anotherSubscriber = new TestEventSubscriber <CustomerAddedEvent>();

            eventAggregator.RegisterSubscriber(anotherSubscriber);

            if (garbageCollect)
            {
                GC.Collect();
            }

            eventAggregator.UnregisterSubscriber(anotherSubscriber);

            Assert.Equal(expectedNumberOfSubscribers, eventAggregator._subscribersByEvent[typeof(CustomerAddedEvent)].Count);
        }
        public void ShouldPushEventsToRightSubscribers()
        {
            var eventAggregator = new EventAggregator();

            var customerAddedEventSubscriber = new TestEventSubscriber <CustomerAddedEvent>();

            eventAggregator.RegisterSubscriber(customerAddedEventSubscriber);

            var customerDeletedEventSubscriber = new TestEventSubscriber <CustomerDeletedEvent>();

            eventAggregator.RegisterSubscriber(customerDeletedEventSubscriber);

            eventAggregator.Publish(new CustomerAddedEvent(5));
            eventAggregator.Publish(new CustomerDeletedEvent(7));

            Assert.Single(customerAddedEventSubscriber.ReceivedEvents);
            Assert.Equal(5, customerAddedEventSubscriber.ReceivedEvents[0].CustomerId);
            Assert.Single(customerDeletedEventSubscriber.ReceivedEvents);
            Assert.Equal(7, customerDeletedEventSubscriber.ReceivedEvents[0].CustomerId);
        }