使用 StaticResources 测试 WPF 窗口 [英] Testing a WPF Window with StaticResources

查看:21
本文介绍了使用 StaticResources 测试 WPF 窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的 Window,其中引用了 App.xaml 中的 StaticResource.

I have a simple Window with a reference to a StaticResource in the App.xaml.

App.xaml 资源定义:

App.xaml resource definition:

<!-- Standard Text Box Style -->
<Style x:Key="textBoxStyleStd" TargetType="{x:Type TextBox}">
    <Setter Property="FontSize" Value="14" />
</Style>

使用资源的窗口组件:

<TextBlock Grid.Column="1" Grid.Row="0" Name="stationIdTitle"
           Style="{StaticResource textBlockStyleStd}"
           VerticalAlignment="Center" HorizontalAlignment="Center"
           Text="{LocText Key=Title, Dict={StaticResource Dictionary},
               Assembly={StaticResource Assembly}}"/>

尝试对这个窗口进行单元测试时出现错误:

When trying to unit test this Window I get the error:

System.Windows.Markup.XamlParseException:找不到名为的资源'{textBlockStyleStd}'.资源名称区分大小写.错误在标记文件中的对象stationIdTitle"'Zpg;component/guicomponenets/screens/enterstationidscreen.xaml' 行23 位置 71.

System.Windows.Markup.XamlParseException: Cannot find resource named '{textBlockStyleStd}'. Resource names are case sensitive. Error at object 'stationIdTitle' in markup file 'Zpg;component/guicomponenets/screens/enterstationidscreen.xaml' Line 23 Position 71.

有什么办法可以解决这个问题吗?我的单元测试代码是:

Is there any way around this? My unit test code is:

[Test]
public void TestEnterKeyPressedNoText()
{
    IPickingBusinessObject pickingBusinessObject = mock.StrictMock<IPickingBusinessObject>();

    EnterStationIdScreen objectUnderTest = new EnterStationIdScreen(pickingBusinessObject);

    Assert.AreEqual(Visibility.Visible, objectUnderTest.stationIdError.Visibility);

    Assert.AreEqual("werwe", "oksdf");

    Replay();

    objectUnderTest.EnterKeyPressed();

    Verify();
}

推荐答案

感谢 Kent,

我查看了您的建议,在大多数情况下,我同意应该使用和测试模型,但是,我仍然想测试一些与控件相关的代码(例如 TextBox 可见性).为了解决这个问题,您可以创建应用程序的一个实例(但不初始化它)并手动添加资源.这确实会导致 App.xaml 和基本单元测试中出现重复,但这使我能够完成我想要的测试.

I looked at your suggestions and in most scenarios I agree models should be used and tested however, there is some code associated with the controls (e.g. TextBox visibility) I still wanted to test. To get round this you can create an instance of your Application (but not initialize it) and add the resources manually. This does lead to duplication in the App.xaml and the base unit test but this allows me to complete the tests I wanted.

        if (Application.Current == null)
        {
            App application = new App();

            #region Add Static Resources from the App.xaml

            Style textBoxStyle = new Style(typeof(TextBox));
            textBoxStyle.Setters.Add(new Setter(TextBox.FontSizeProperty, 14d));

            Style textBlockStyle = new Style(typeof(TextBlock));
            textBlockStyle.Setters.Add(new Setter(TextBlock.FontSizeProperty, 14d));

            application.Resources.Add("TextBoxStyleStd", textBoxStyle);
            application.Resources.Add("TextBlockStyleStd", textBlockStyle);
            application.Resources.Add("TextBlockStyleError", textBlockStyle);
            application.Resources.Add("Assembly", "Zpg");

            #endregion
        }       

这篇关于使用 StaticResources 测试 WPF 窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆