SampleConfirconationDialog可以进行哪些方法进行测试? SampleConfirmationdialog将通过接受测试行使,但是我们如何将其单位测试,因为消息框不是抽象的,也没有匹配的接口?

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;
    }
}
有帮助吗?

解决方案

您不能,在当前状态下它是无法测试的。对于此特定类,单位测试也没有价值...它只是内置框架功能周围的轻覆盖者,因此您要做的就是测试框架。

如果您绝对必须对其进行测试,则ICONFIRMATIONDIAGOG接口应具有另一个可以在单元测试中模拟的依赖项。

其他提示

您应该研究Typemock,这是一个商业模拟框架,可让您使用.NET性能分析库来测试此类情况。看 他们的网站 了解更多信息。

我认为可以停止在该级别进行测试。您与之互动 IConfirmationDialog 比验证 MessageBox.Show 实际上被打电话了。由于那是一个界面,很容易嘲笑,我认为您的覆盖范围很好。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top