I have some unit tests that I'm writing for a WPF application, and as much as I've tried to avoid it, I have some code under test that instantiates a View. As soon as the view is instantiated, all the markup extensions, styles, etc are evaluated. To resolve this I've created a dummy Application and registered any required resources when the test assembly is initialized:
[TestClass]
public class AssemblyInitialize
{
[AssemblyInitialize]
public static void SetupTestAssembly(TestContext context)
{
if (Application.Current == null)
new Application();
var resources = new List<string>
{
"pack://application:,,,/AssemblyName;component/ResourceDictionary.xaml"
};
foreach(var resource in resources)
{
var uri = new Uri(resource);
var dictionary = new ResourceDictionary { Source = uri };
Application.Current.Resources.MergedDictionaries.Add(dictionary);
}
}
}
I've used this approach in the past, and it works ok.
I've run into a small snag with this approach. I have a few resources that use pack://siteoforigin: in the pack Uri, and when the tests instantiate this view I get an error about not being able to resolve the file.
The XAML:
<ResourceDictionary
xmlns="...">
<ImageBrush
x:Key="ResourceName"
ImageSource="pack://siteoforigin:,,,/Resources/image.png"
/>
</ResourceDictionary>
Error Message:
Could not find a part of the path 'C:\\Solution\\TestResults\\Workspace_2012-03-01 14_54_29\\Resources\\image.png'
I've added the Resources directory as a deployment item, and I've confirmed that the image is the TestRun output directory. It seems that the AppDomain is operating one folder above where the location of my test assemblies, because the file is actually located at:
c:\Solution\TestResults\Workspace_2012-03-01 14_54_29\ Out \Resources\image.png
Any suggestions on how I can get the WPF Application to use the Out directory as it's primary folder?
AppDomain.CurrentDomain.SetData("APPBASE", "FolderNameHere");
It seems like there is a better way, but my memory's failing me. This may be Good Enough (tm). – ianschol