Replace instance of IMessageService at ViewModelLocator in unit test method that calls MessageDialog RRS feed

  • Question

  • Hi,

    I have developed a WinRT app (C# XAML) using the pattern MVVM.

    The solution has several unit test, and I want to test methods that internally maybe show a messageDialog.

    The MainViewModel has in its constructor injected several services like, IConfigurationService, ICacheService and also IMessageService.

    IMessageService has to implementations, the first one "MessageService" with var messageDialog = new MessageDialog(str); and await messageDialog.ShowAsync(); and the second one "DesignMessageService" with a fake task.

    So, when I run the tests all tests related with methods that use MessageDialog fails, because the ViewModelLocator gives me the MessageService implementation (which is correctly) instead of the DesignMessageService that I need in that case.

    I want a way to replace the instance that ServiceLocator give to the [Fact] method to be able to pass the test.

    So, any idea?

    This problem can be solved in other way, for example, not using a IMessageService in the ctor of the mainviewModel and call directly in the MainViewModel a Helper like Helper.ShowMessageDialog(str) and this method internally pass to the View the string and a propdp that enables and disables the visibility of a custom message dialog like a grid.

    Any advice is welcome.


    Thursday, August 6, 2015 9:46 AM

All replies

  • Hi Tacheleu,

    I am trying to involve someone familiar with this topic to further look at this issue. There might be some time delay. Appreciate your patience.

    Best Regards,

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, August 7, 2015 5:42 AM
  • Hi Tina,

    thanks for your interest. I hope some skilled guy can give us an advice.

    I have a possible solution, but I think that it is no the good one.

    In the [Fact] method, I create a new instance of the MainViewModel (vm = new MainViewModel()), passing thorugh parameter all its dependencies. All its dependencies are the real implementation of its own interfaces except for IMessageService. In that case I use DesignMessageService. And thats all.

    The test method executes Assert.NotNull(vm) and followed by vm.someCommand.Execution(parameter).

    This works, because vm instance has been injected DesignMessageService, so no popup/modal has launched in the execution of the [Fact] method.

    Anyway, I am waiting for better solution, like for example, this method (like the other ones in the test class) can use the instance return of new ViewModelLocator() (executed in the ctor of the test class); and somehow change/replace the instance MessageService for DesignMessageService. I think that, if it can be done, will be more clean solution.

    Thanks again

    Friday, August 7, 2015 7:49 AM