Unit testing the view model that is based on prism and requires region manager, composition container to be registered

StackOverflow https://stackoverflow.com/questions/20699938

  •  20-09-2022
  •  | 
  •  

Question

I am Unit testing view model that uses prism. While calling a constructor of view model class i am required to pass region manager, composition container. How to initialize region manager in the unit Test setup.

Was it helpful?

Solution

When unit testing you view models you will need to create dummy implementations for the view model's dependencies. First of all check that you are using interfaces and not classes to when passing the dependencies to your view model. For example, instead of this:

public MyViewModel(RegionManager rm, UnityContainer container) { ... }

You can use interfaces like this:

public MyViewModel(IRegionManager rm, IUnityContainer container) { ... }

Then, you can create dummy classes that implement those interfaces. If you are using a dependency injection container in your tests too, you can also export the dummy classes to it by mapping the interfaces to their corresponding implementations.

For the particular case of the Region Manager you can create a simple implementation of the IRegionManager interface that will simply host a collection of dummy IRegions to be used in your view model. Like this, no UI is required to test the view model's behavior.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top