3

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.

verticalLineStyle 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?

2 Answers 2

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

This helped me.

Sign up to request clarification or add additional context in comments.

2 Comments

In UWP apps for Win 10 this overload doesn't exist. Anyone know a) what to pass as the first parameter and b) what the URI would be for an UWP's App.xaml?
The assembly name can be found in project's properties, and the 'component/' part it litterally 'component'.
0

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.