Question

I have a class that uses constructor DI for IEventAggregator

 public SomeViewModel(IEventAggregator eventAggregator)
        {           
            this.eventAggregator = eventAggregator;
            eventAggregator.GetEvent<SomethingChanged>().Subscribe(UpdateResults);         
        }  

I have another method in the same class that executes some logic that is not using eventAggregator.

public void SomeMethod()
{
   //Necessary logic
}

Now I need to create a unit test for this method. I have a different test project of type class library.

I created a mock object using Moq

var someObject = new SomeViewModel(new Mock<IEventAggregator>())
someObject.SomeMethod();

This requires adding the prism reference to my Test Project. Is this the right way? or can I create an object for SomeViewModel without using Moq and IEventAggregator?

Was it helpful?

Solution

So the problem is that IEventAggregator is part of prism. So any of your objects that use it have a dependency on that library.

In the case of your unit tests its not really a problem. It doesnt matter if they reference a few extra libraries.

But, if you wanted to use your same objects in say a web version of your application. Then the prism library would be awkward and unneeded.

Try keep your business objects separate from the implementation of logic which requires specific libraries.

So in this case for example its fine to use prism in your view models, they are tied to your WPF app which in turn uses prism. But you would want to keep your Models and Interfaces free of prism references.

PS. Obviously as with any 'best practice' you are free to cut corners where expedient

Licensed under: CC-BY-SA with attribution
scroll top