How to Make Unit Tests Aware of Application.Resources

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

From App.xaml (in the order of this SO answer ):

App.xaml:

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

In this dictionary, I have several styles, including the 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(); } } 

This setting works very well when Visual Studio views the page in design mode and when the project starts.

I am using Visual Studio built in Unit Test tools . When I try to run unit tests for the MyPage class (which uses MyPage.xaml when running the constructor), my tests fail. MyPage.xaml uses the styles defined in the dictionary included in Application.Resources. Tests do not recognize the PageStyle parameter because Application.Resources did not turn on when starting Unit Test, and as a result, the page was not created. How to enable my applications. Resources in my unit tests? Also, is there a better way to run unit tests for WPF pages and windows?

+4
source share
2 answers

Based on your comment, I suggest you use Model-View-ViewModel (MVVM) or any other scheme to get as much logic as possible from your Components at the GUI level and test this logic. This does not directly solve your current problem, but the fact is that the GUI logic, as you know, is difficult to verify - as complicated as it is, in my opinion, with MS Test and other unit test runners.

Unit GUI testing is a world of pain (I tried). There are usually better tools for this.

+6
source

I developed some code that works and actually loads a real element that was missing for me, although the fooobar.com/questions/461040 / ... approach is probably better for isolating the error test and creating a test library.

 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(); 
+2
source

Source: https://habr.com/ru/post/1394505/


All Articles