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.