Issue with ImoduleManager during unit tetsing in prism wpf app

Topics: Prism v2 - WPF 3.5, Prism v2 - WPF 4, Prism v4 - Silverlight 4, Prism v4 - WPF 4
Oct 24, 2013 at 1:54 PM
Hi,

We are using unity,Prism in our wpf appp. In one of our view model
we have a constructor which accepts ImoduleManager , During runtime , this value is set property. But how to set/initialize it during unit tests. Whenever we run unit tests, it throws erro. How to create instance of ImoduleManger via unit tests. please suggest


For e;g: In code , UserControlViewModel([Dependency] IModuleManager moduleManager)

Kind Regards
Krishna
Oct 24, 2013 at 2:16 PM
Hi,

Use a mocking framework like Moq, Rhinomocks to mock the IModuleManager and pass that to your viewmodel. Read up more on how to mock methods.
http://code.google.com/p/moq/

Cheers!
Ganesh
Oct 24, 2013 at 2:55 PM
Hi Krishna,

Based on my understanding the module manager gets instantiated when the Bootstrapper is ran. Therefore, the IModuleManager instance is retreived on the constructor from the Container.

As Ganesh correctly said, you may create a mock instance of IModuleManager which would be then passed to the ViewModel constructor. The following implementation shows how to create the IModuleManager mock and the corresponding ViewModel (You would need to use Microsoft.Practices.Prism.Modularity namespace for IModuleManager):
var moduleManagerMock = new Mock<IModuleManager>();
var viewModel = new MyViewModel(moduleManagerMock.Object);
I hope this helps,

Gabriel Ostrowsky
https://blog.southworks.net/gostrowsky
Oct 25, 2013 at 6:57 AM
Thanks for your reply . We are not using Moq or any other framework for unit testing . How to instatiate ImoudleManager in normal way . I guess it accepts 3 more parameters . Is there a way to do it or Moq is the only way to do it.
Oct 25, 2013 at 7:44 AM
Hi,

Ideally when testing your ViewModel you should mocking external references, as the purpose of your unit testing is to test the viewmodel behavior and not how IModuleManager works. So mocking is the only good way to go about doing this.

Cheers!