2

SampleConfirmationDialog 可以通过哪些方式进行单元测试?SampleConfirmationDialog 将通过验收测试来执行,但是我们如何对它进行单元测试,因为 MessageBox 不是抽象的并且没有匹配的接口?

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;
    }
}
4

3 回答 3

5

你不能,它在当前状态下是不可测试的。对于这个特定的类,对它进行单元测试也没有任何价值……它只是一个围绕内置框架特性的轻量级包装器,所以你要做的就是测试框架。

如果你绝对必须测试它,IConfirmationDialog 接口应该有另一个依赖项,你可以在单元测试中模拟它。

于 2010-05-20T21:20:52.190 回答
0

您应该研究 Typemock,这是一个商业模拟框架,可让您使用 .NET 性能分析库对这些情况进行单元测试。有关更多信息,请参阅他们的网站

于 2010-05-21T03:16:11.957 回答
0

我认为在那个级别停止测试它是可以的。您的交互IConfirmationDialog比验证MessageBox.Show实际被调用更重要。由于这是一个界面并且很容易模拟,我认为你已经很好地覆盖了。

于 2010-05-21T03:19:52.107 回答