I am developing a WPF application with the MVVM pattern.
I know that the MVVM pattern is good to seprate the GUI and the logic of the application, so I can test the GUI and the logic independently.
However, if I need to send some messages to the user, is it a good idea to use a messageBox or not? Because I am reading about some solutions, all of them try to implement a new view a its view model… etc. But I don’t see very well the utility.
I mean that if I want to notify something to the user, my results depends on the decision of the user, how can I separate my tests (GUI and logic)? What is the benefit of not using directly a messageBox instead a new view with the viewmodel and all the extra work that this requiered? Because in both cases my results depends of the user decision. How can I test my application in this case?
How do you unit test a method which is opening message boxes?
What if the specification changes and you need to use a different (custom) message box, or even only log an error (to be displayed in a summary report later on)? Then you need to find and replace each and every
msgBox.Showcall.By creating an event, which is triggered when you need a decision to be made. Then you’re getting the decision back. You don’t care where it came from.
Very simply. You just mock your user reply. You can (and probably should) test both possible scenarioes, so just attach two “fake” event handlers: one returning positive decision, one returning negative decision, as if your user actually clicked “Yes” or “No” in some messagebox.
See http://joyfulwpf.blogspot.com/2009/05/mvvm-communication-among-viewmodels.html for example.
ASpirin‘s suggestion of injecting a notifier is also a good design choice.
Some sketchy, oversimplified implementation: