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?

asked Jan 18, 2019 at 4:19
1
  • 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. Commented Jan 18, 2019 at 6:03

1 Answer 1

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

answered Jan 18, 2019 at 5:10

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.