Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8356057
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T10:03:48+00:00 2026-06-09T10:03:48+00:00

I am developing a WPF application with the MVVM pattern. I know that the

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T10:03:52+00:00Added an answer on June 9, 2026 at 10:03 am

    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.

    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.Show call.

    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)?

    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.

    Because in both cases my results depends of the user decision. How can
    I test my application in this case?

    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:

    using System;
    using System.Windows.Forms;
    
    namespace Demo
    {
        public delegate bool DecisionHandler(string question);
        /// <remarks>
        /// Doesn't know a thing about message boxes
        /// </remarks>    
        public class MyViewModel
        {
            public event DecisionHandler OnDecision;
    
            private void SomeMethod()
            {
                // something...
    
                // I don't know who or what replies! I don't care, as long as I'm getting the decision!
                // Have you made your decision for Christ?!! And action. ;)
                var decision = OnDecision("Do you really want to?"); 
    
                if (decision)
                {
                    // action
                }
                else
                {
                    // another action
                }
            }
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                // this ViewModel will be getting user decision from an actual message box
                var vm = new MyViewModel();
                vm.OnDecision += DecideByMessageBox;
    
                // unlike that one, which we can easily use for testing purposes
                var vmForTest = new MyViewModel();
                vmForTest.OnDecision += SayYes;    
            }        
    
            /// <summary>
            /// This event handler shows a message box
            /// </summary>        
            static bool DecideByMessageBox(string question)
            {
                return MessageBox.Show(question, String.Empty, MessageBoxButtons.YesNo) == DialogResult.Yes;
            }
            /// <summary>
            /// Simulated positive reply
            /// </summary>        
            static bool SayYes(string question)
            {
                return true;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm developing a networked WPF application with the MVVM pattern and it seems that
I'm developing a WPF application using the MVVM pattern and I need to display
I'm in the early phases of developing a WPF application (using the MVVM pattern)
I am developing an application in WPF using the MVVM pattern. If I have
I am developing a WPF application using the MVVM pattern as described by Josh
I am developing an application that uses Entity Framework and WPF with MVVM design
I'm developing a .NET 4.0 application using PRISM and MVVM, as well as WPF.
I am developing a WPF application, that connects to several WCF services (that work
I am developing a WPF application that must run using Windows Classic theme. The
I am developing a WPF application that must meet Section 508 (Accessibility) requirements. In

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.