如何使单元测试了解Application.Resources

for*_*ord 6 c# wpf xaml unit-testing visual-studio-2010

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

从App.xaml(以此SO响应的方式):

App.xaml中:

<Application.Resources>    
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>                
            <ResourceDictionary 
                  Source="MyDictionary.xaml">
                </ResourceDictionary>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>
Run Code Online (Sandbox Code Playgroud)

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

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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

MyPage.xaml.cs

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

当Visual Studio在设计模式下查看页面和运行项目时,此设置都能很好地工作.

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

Mas*_*low 7

我已经制定了一些有效的代码并实际加载了我缺少的真实项目,尽管/sf/answers/52390831/的方法可能更适合隔离测试错误和构建测试库.

var targetAssembly = typeof(PracticeManagement.MainWindow).Assembly;
var currentAssembly = this.GetType().Assembly;

Application.ResourceAssembly = targetAssembly;

var rd = (ResourceDictionary)Application.LoadComponent(new Uri("/ProjectName;component/CommonUIStylesDictionary.xaml", UriKind.Relative));

if (Application.Current == null)
{
    var x = new System.Windows.Application(); // magically is assigned to application.current behind the scenes it seems
}

Application.Current.Resources = rd;

var mainWindow = new ProjectName.MainWindow(this.Connection.ConnectionString);

mainWindow.Show();
Run Code Online (Sandbox Code Playgroud)


Eri*_*ich 6

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

单元测试GUI是一个痛苦的世界(我已经尝试过).通常有更好的工具.


小智 5

看来创建应用程序类的实例并调用 InitializeComponent 就可以解决问题。

var app = new App(); //magically sets Application.Current
app.InitializeComponent(); //parses the app.xaml and loads the resources
Run Code Online (Sandbox Code Playgroud)