public void TestParentAndChildSubscription()
 {
     var hub = this.GetEventHub();
     var args = new EventMock(10);
     int called = 0;
     Action<EventMock> handler = (e => called++);
     hub.Subscribe(handler);
     hub.Subscribe<EventMockChild>(handler);
     hub.Raise(args);
     called.Should().Be(1, "event handler should be called exactly once");
 }
 public void TestEventInterface()
 {
     var hub = this.GetEventHub();
     var args = new EventMock(10);
     int called = 0;
     hub.Subscribe<IEvent>(e =>
     {
         e.Should().BeSameAs(args, "the passed arguments should be the same as provided");
         called++;
     });
     hub.Raise(args);
     called.Should().Be(1, "event handler should be called exactly once");
 }
 public void TestDoubleSubscription()
 {
     var hub = this.GetEventHub();
     var args = new EventMock(10);
     int called = 0;
     Action<IEvent> handler = (e => called++);
     hub.Subscribe(handler);
     hub.Subscribe(handler);
     hub.Raise(args);
     called.Should().Be(2, "event handler should be called exactly twice");
 }
 public void TestNoSubscription()
 {
     var hub = this.GetEventHub();
     var args = new EventMock(10);
     hub.Raise(args);
 }
        public void TestHandlerUnsubscribingItself()
        {
            var hub = this.GetEventHub();
            var args = new EventMock(10);

            int beforeHandlerCalled = 0;
            Action<IEvent> beforeHandler = (e => beforeHandlerCalled++);
            hub.Subscribe(beforeHandler);

            int unsubscribingHandlerCalled = 0;
            Action<IEvent> unsubscribingHandler = null;
            unsubscribingHandler = (e => { unsubscribingHandlerCalled++; hub.Unsubscribe(unsubscribingHandler); });
            hub.Subscribe(unsubscribingHandler);

            int afterHandlerCalled = 0;
            Action<IEvent> afterHandler = (e => afterHandlerCalled++);
            hub.Subscribe(afterHandler);

            hub.Raise(args);
            beforeHandlerCalled.Should().Be(1, "before event handler should be called exactly once after the first raise");
            unsubscribingHandlerCalled.Should().Be(1, "unsubscribing event handler should be called exactly once after the first raise");
            afterHandlerCalled.Should().Be(1, "after event handler should be called exactly once after the first raise");

            hub.Raise(args);
            beforeHandlerCalled.Should().Be(2, "before event handler should be called exactly twice");
            unsubscribingHandlerCalled.Should().Be(1, "unsubscribing event handler should not be called after the second raise since it has unsubscribed itself during the first");
            afterHandlerCalled.Should().Be(2, "after event handler should be called exactly twice");
        }
 public void TestUninitialize()
 {
     var hub = this.GetEventHub();
     var args = new EventMock(10);
     int called = 0;
     Action<EventMock> handler = (e => called++);
     hub.Subscribe(handler);
     hub.Raise(args);
     called.Should().Be(1, "event handler should be called exactly once after the first raise");
 }