mercredi 30 mars 2016

Testing Prism event subscription and unsubscription count in Nsubstitute

I have few events that derive from a base class. I want to write a unit test that will verify that all the events were unsubscribed when navigating away from the page. As every event is a different derive type, Is there a way to use the base class to get the number of calls?

Class code 
private void SubscribeToEvent()
    {
        EventAggregator.GetEvent<RepositoryObjectAdded>().Subscribe(HandleTestGroupNodeAdded, ThreadOption.UIThread);
        EventAggregator.GetEvent<RepositoryObjectRemoved>().Subscribe(HandleTestNodeDeleted, ThreadOption.UIThread);
    }

private void UnsubscribeEvents()
    {
        EventAggregator.GetEvent<RepositoryObjectAdded>().Unsubscribe(HandleTestGroupNodeAdded);
        EventAggregator.GetEvent<RepositoryObjectRemoved>().Unsubscribe(HandleTestNodeDeleted);

    }

Test Class Code:
    [Fact]
    public void OnNavigatedFrom_UnsubscriptionCount()
    {
        NavigationContext navigationContext = Substitute.For<NavigationContext>(null, null);
        target.OnNavigatedFrom(navigationContext);
        CtorParams.EventAggregator.Received(EventSubscriptionCount).GetEvent<PubSubEvent<IUniqueObject>>().Unsubscribe(Arg.Any<Action<IUniqueObject>>());
    }

public class RepositoryObjectAdded : PubSubEvent<IUniqueObject>
{ 
}

public class RepositoryObjectRemoved : PubSubEvent<IUniqueObject>
{ 
}

Aucun commentaire:

Enregistrer un commentaire