Telerik blogs

Using JustMock Commercial Edition you can easily mock out calls to MessageBox.Show().  This can come in handy if you are testing code that bases its execution path on a MessageBox’s result. Also, it is unlikely you want the MessageBox to actually pop up while running your tests.  Let’s see how we can accomplish this! 

The Basics

[TestMethod]
public void MOCK_MESSAGEBOX_RESULT()
{
 bool called = false;
    DialogResult expected = DialogResult.Ignore;
 
    Mock.Arrange ( () => MessageBox.Show ( Arg.AnyString ) )
        .DoInstead ( () => called = true )
        .Returns ( expected );
 
    var result = MessageBox.Show ( "HELLO!" );
 
    Assert.IsTrue ( called );
    Assert.AreEqual(expected, result);
}

The first thing I do is create a variable to hold the expected MessageBox result.  Then I arrange the static Show(string) method of MessageBox. I tell the method to set a local variable “called” to true instead of calling the Show() method’s original implementation by using JustMocks DoInstead feature. Following that we tell the method to return our expected result, DialogResult.Ignore in this case.  

The line after Mock.Arrange actually calls MessageBox.Show passing in a test string.  Normally when a MessageBox.Show is a little modal form would pop up, and would require the user to click the “OK” button, however using JustMock we have changed the Show method’s execution path, and all MessageBox.Show will do during this test is set the “called” variable to true.  We wrap up the test by asserting our expectations.  And we get green! 

image

 

“Real World” Example

The previous test shows us that JustMock can successfully arrange MessageBox.Show, but it doesn’t do much besides test the JustMock framework.  So lets see how we can apply this to a real scenario. 

Lets say I have the following form:

public partial class Form1 : Form
{
 public Form1()
   {
       InitializeComponent();
   }
 
 public bool DoIt () {
 
       var result = MessageBox.Show ( "HELLO!", "Test Message", MessageBoxButtons.OKCancel );
 if (result == System.Windows.Forms.DialogResult.OK)
       {
 return true;
       }
 else
       {
 return false;
       }
   }
} 

As we can see there are two paths through the DoIt method in our form.  I want to test BOTH paths, but the execution path is based on the pesky DialogResult the MessageBox returns!  Using the code above we can quickly create tests that will navigate us down both execution paths for the DoIt method. 

[TestMethod]
public void DOIT_SHOULD_RETURN_TRUE_WHEN_MESSAGEBOX_RETURNS_OK_RESULT()
{
 bool called = false;
   DialogResult  expected  = DialogResult.OK;
 
   Mock.Arrange(() => MessageBox.Show(Arg.AnyString, Arg.AnyString, MessageBoxButtons.OKCancel))
       .DoInstead(() => called = true)
       .Returns(expected);
 
   var form1 = new Form1 ( );
   var result = form1.DoIt ( );
 
   Assert.IsTrue ( result );
   Assert.IsTrue ( called );
}
 
[TestMethod]
public void DOIT_SHOULD_RETURN_FALSE_WHEN_MESSAGEBOX_RETURNS_CANCEL_RESULT()
{
 bool called = false;
   DialogResult expected = DialogResult.Cancel;
 
   Mock.Arrange(() => MessageBox.Show(Arg.AnyString, Arg.AnyString, MessageBoxButtons.OKCancel))
       .DoInstead(() => called = true)
       .Returns(expected);
 
   var form1 = new Form1();
   var result = form1.DoIt();
 
   Assert.IsFalse(result);
   Assert.IsTrue(called);
}

The test code is slightly longer in these tests as we are mocking out the MessageBox.Show overload that allows you to specify the buttons it will show.  The first test simulates the user hitting the “OK” button on the messagebox, and the second test simulates the user hitting “Cancel.”

 

To run these tests you will need the commercial version of JustMock, and make sure to enable the profiler:

image

 

Hopefully this helps you out in the future!  Happy Coding! 

Download Code


Comments

Comments are disabled in preview mode.