Unit Testing: hard dependency MessageBox.Show()

Posted by Sean B on Stack Overflow See other posts from Stack Overflow or by Sean B
Published on 2010-05-20T21:15:51Z Indexed on 2010/05/20 21:20 UTC
Read the original article Hit count: 195

Filed under:
|
|
|
|

What ways can the SampleConfirmationDialog be unit tested? The SampleConfirmationDialog would be exercised via acceptance tests, however how could we unit test it, seeing as MessageBox is not abstract and no matching interface?

public interface IConfirmationDialog
{
    /// <summary>
    /// Confirms the dialog with the user
    /// </summary>
    /// <returns>True if confirmed, false if not, null if cancelled</returns>
    bool? Confirm();
}


/// <summary>
/// Implementation of a confirmation dialog
/// </summary>
public class SampleConfirmationDialog : IConfirmationDialog
{
    /// <summary>
    /// Confirms the dialog with the user
    /// </summary>
    /// <returns>True if confirmed, false if not, null if cancelled</returns>
    public bool? Confirm()
    {
        return MessageBox.Show("do operation x?", "title", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes;
    }
}

© Stack Overflow or respective owner

Related posts about unit-testing

Related posts about wpf