开发者

WPF - XamlParseException when unit testing method in Window class

开发者 https://www.devze.com 2023-01-18 02:13 出处:网络
I\'m trying to use MSTest to write unit tests for methods in my MainWindow class in my WPF application.However, I get the following exception:

I'm trying to use MSTest to write unit tests for methods in my MainWindow class in my WPF application. However, I get the following exception:

System.Windows.Markup.XamlParseException: 'Provide value on 'System.Windows.StaticResourceExtension' threw an exception.' Line number '127' and line position '32'. ---> System.Exception: Cannot find resource named 'verticalLineStyle'. Resource names are case sensitive.

verticalLineS开发者_开发技巧tyle is found in a ResourceDictionary that is included in App.xaml. I have no need of this style to test the method I'm trying to test, but I can't get past this. I'm just creating a new instance of MainWindow in my unit test and trying to call an instance method on that. The exception occurs when I try to create the instance of MainWindow. MainWindow is defined as public partial class MainWindow : Window.

How do I get rid of this error? Is there some preferred way of unit testing methods in windows in WPF applications?


Application.LoadComponent(
    new Uri("/MyAppAssemblyName;component/App.xaml", UriKind.Relative));

This helped me.


The problem is that the test does not have access to App.xaml as the application is not started. You might want to look into TestApi for anything related to testing WPF visuals (link)

Hope this helps!

0

精彩评论

暂无评论...
验证码 换一张
取 消