0

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?

  • References are effectively transitive anyway; since your library under test already has the reference to Prism, adding it to the test project doesn't matter. – Sebastian Redl Jan 18 at 6:03
0

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

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.