【问题标题】:WPF Custom Control - How do you unit test a custom control?WPF 自定义控件 - 如何对自定义控件进行单元测试?
【发布时间】:2011-04-01 16:23:48
【问题描述】:

基本上,我正在寻找有关如何对 WPF 自定义控件进行单元测试的资源/指南。

在这个特定的实例中,我创建的自定义控件恰好扩展了Decorator 类。它包装了一个 PasswordBox 子项,以将 CLR 密码属性公开为 DependencyProperty。

public class BindablePasswordBox : Decorator
{
    public BindablePasswordBox()
    {
        Child = new PasswordBox();
        ((PasswordBox)Child).PasswordChanged += this.PasswordChanged;
    }

    public static readonly DependencyProperty PasswordProperty =
        DependencyProperty.Register("Password", typeof(String), typeof(BindablePasswordBox),
            new FrameworkPropertyMetadata
            {
                BindsTwoWayByDefault = true,
                DefaultUpdateSourceTrigger = UpdateSourceTrigger.LostFocus
            });

    public String Password
    {
        get { return (String)GetValue(PasswordProperty); }
        set { SetValue(PasswordProperty, value); }
    }

    void PasswordChanged(Object sender, RoutedEventArgs e)
    {
        Password = ((PasswordBox)Child).Password;
    }
}

附:我正在使用内置的 Visual Studio 测试框架 (Microsoft.VisualStudio.QualityTools.UnitTestFramework)。


为了避免在内存中公开明文密码引起强烈反对:我知道我通过在 DependencyProperty 中公开明文密码来违反 Microsoft 的安全推理,但考虑到我能够使用 Snoop 公开明文密码从一个标准的 PasswordBox 我觉得它不再那么重要了。

【问题讨论】:

    标签: wpf unit-testing custom-controls dependency-properties


    【解决方案1】:

    您可以使用 UI 自动化,有关详细信息,请参阅以下链接:

    UI Automation Overview

    UI Automation of a WPF Custom Control

    【讨论】:

    • UI 自动化与单元测试不同。
    猜你喜欢
    • 2011-02-26
    • 2021-08-08
    • 2018-01-14
    • 2011-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多