1

Thanks to this SO answer I've managed to test publish event of PRISM EventAggregator (faking with FakeItEasy)

[TestCase]
public void test_that_publish_occured()
{
    var fakeEventAg = A.Fake<IEventAggregator>();
    var fakeEvent = A.Fake<MyEvent>();

    A.CallTo(() => fakeEventAg.GetEvent<MyEvent>())
      .Returns(fakeEvent);

    MyViewModel mvm = new MyViewModel(fakeEventAg);

    mvm.ICommandThatCausesPublishToBeCalled.Execute();

    A.CallTo(() => fakeEvent.Publish(A<SomeClass>.Ignored))
      .MustHaveHappened();
}

But I have failed to test subscribe to this event. I have tried the following but I get an exception "Non virtual methods can not be intercepted".

[TestCase]
public void test_that_event_is_listened()
{
    var fakeEventAg = A.Fake<IEventAggregator>();
    var fakeEvent = A.Fake<MyEvent>();
    A.CallTo(() => fakeEventAg.GetEvent<MyEvent>())
      .Returns(fakeEvent);

    // subscription occurs in the constructor
    MyViewModel2 mvm2 = new MyViewModel2(fakeEventAg);

    A.CallTo(() => fakeEventAg.GetEvent<MyEvent>()
      .Subscribe(A<Action<PayloadClass>>.Ignored))
      .MustHaveHappened();
}

How can I test that a subscription to an event has occurred? It doesn't have to be unit test, but can also be integration test.

Community
  • 1
  • 1
bitman
  • 547
  • 2
  • 6
  • 23
  • 1
    The error is happening because the `Subscribe` method is not virtual and therefore can't be mocked. I see that the `EventBase` class (which `MyEvent` is based on) has a `Subscriptions` property. Perhaps you could examine that collection to verify that your event has been added? – Jack A. Jan 18 '16 at 14:35
  • yes, the Subscriptions property works if I use real EventAggregator. With a faked EA it remains 0. But I can do the test also with real EA, so it solves my problem. Thanks! If you formulate your comment into an answer, then I can also mark it as selected answer! – bitman Jan 19 '16 at 07:49

1 Answers1

1

The error is happening because the Subscribe method is not virtual and therefore can't be mocked. I see that the EventBase class (which MyEvent is based on) has a Subscriptions property. You should be able to examine that collection to verify that your event has been added.

Jack A.
  • 4,245
  • 1
  • 20
  • 34
  • of course I had to use reflection to get the Subscriptions property, but I guess that's ok in a test... – bitman Jan 19 '16 at 13:18
  • What class is your event actually derived from? I was assuming that it was something like `CompositePresentationEvent`. The `EventBase` class doesn't actually implement a `Subscribe` method, but the derived classes do. If you are actually implementing the `Subscribe` method, you could change it to be virtual just for the sake of mocking. Alternatively, the `EventBase` class has a protected virtual `InternalSubscribe` method that may be mockable (don't know if Moq does protected methods but I would assume that it does). – Jack A. Jan 19 '16 at 14:09
  • `MyEvent` derives from `PubSubEvent`, which in turn derives from `EventBase`. I don't implement `Subscribe` myself, but rely on PRISM. Overriding `Subscribe` would defenitely be an option, but in this case it was better to use reflection to get `Subscriptions`. – bitman Jan 19 '16 at 14:52