如何让单元测试知道 Application.Resources [英] How to make Unit Tests aware of Application.Resources

查看:25
本文介绍了如何让单元测试知道 Application.Resources的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 WPF 项目的 Application.Resources 区域中包含一个 ResourceDictionary.这个

I have a ResourceDictionary included in my Application.Resources area of my WPF project. This

来自 App.xaml(按照此 SO 响应的方式):

From App.xaml (in the manner of this SO response):

App.xaml:

<Application.Resources>    
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>                
            <ResourceDictionary 
                  Source="MyDictionary.xaml">
                </ResourceDictionary>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

在这本字典中,我有几种样式,包括一个页面样式:

Within this dictionary, I have several styles, including a Page style:

MyDictionary.xaml:

<SolidColorBrush x:Key="PageBackgroundBrush" 
                     Color="Black" />

<Style x:Key="PageStyle" TargetType="Page">
        <Setter Property="Background" Value="{StaticResource ResourceKey=PageBackgroundBrush}" />
        <Setter Property="Height" Value="Auto" />
        <Setter Property="Width" Value="Auto" />
    </Style>

MyPage.xaml:

<Page x:Class="MyProject.MyPage"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      mc:Ignorable="d" 
      d:DesignHeight="300" d:DesignWidth="250"
      Title="Page"
      Style="{StaticResource ResourceKey=PageStyle}"
      >
<Grid>
</Grid>
</Page>

MyPage.xaml.cs

public class MyPage : Page
{
    public MyPage() 
    {
        // this part crashes during unit tests because 'PageStyle' was not found
        InitializeComponent();
    }
}

此设置在 Visual Studio 在设计模式下查看页面和运行项目时都非常有效.

This setup works quite well both when Visual Studio views the page in design mode and when running the project.

我正在使用 Visual Studio 的内置单元测试工具.当我尝试为 MyPage 类(它在构造函数触发时使用 MyPage.xaml)进行单元测试时,我的测试失败了.MyPage.xaml 使用 Application.Resources 中包含的字典中定义的样式.测试无法识别 PageStyle,因为单元测试开始时未包含 Application.Resources,因此页面无法实例化.如何在我的单元测试中包含我的 Application.Resources?或者,是否有更好的方法来为 WPF 页面和窗口运行单元测试?

I am using Visual Studio's built in Unit Test tools . When I attempt to make unit tests for the MyPage class (which uses MyPage.xaml when the constructor fires) my tests fail. MyPage.xaml makes use of the styles defined in the dictionary included in Application.Resources. The tests do not recognize the PageStyle, because Application.Resources was not included when the unit test began, and as a result the page fails to instantiate. How do I include my Application.Resources in my unit tests? Alternatively, is there a better way to run unit tests for WPF pages and windows?

推荐答案

根据您的评论,我建议您使用 Model-View-ViewModel (MVVM) 或任何其他方案,从您的 GUI 级组件中提取尽可能多的逻辑,并在那里测试该逻辑.它并没有直接解决您当前的问题,但事实是 GUI 逻辑是出了名的难以测试 - 在我看来,使用 MS Test 和其他单元测试运行器测试如此困难以至于不值得.

Based on your comment, I would suggest that you use Model-View-ViewModel (MVVM) or any other scheme to pull as much logic as possible out of your GUI-level components, and testing that logic there. It doesn't directly solve your current problem, but the fact is that GUI logic is notoriously difficult to test - so difficult as not to be worth it, in my opinion, with MS Test and other unit test runners.

对 GUI 进行单元测试是一个痛苦的世界(我已经尝试过).通常有更好的工具可以做到这一点.

Unit testing the GUI is a world of pain (I've tried). There are generally better tools for this.

这篇关于如何让单元测试知道 Application.Resources的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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