使用siteoforigin包Uri进行WPF应用程序的单元测试 [英] Unit Testing WPF Application with siteoforigin pack Uri

查看:423
本文介绍了使用siteoforigin包Uri进行WPF应用程序的单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些我正在为WPF应用程序编写的单元测试,并且尽我所能避免,我有一些正在测试中的实例化View的代码.实例化视图后,将立即评估所有标记扩展,样式等.为了解决这个问题,我创建了一个虚拟的 Application 并在初始化测试程序集时注册了所有必需的资源:

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.

我用这种方法遇到了一个小障碍.我有一些资源在包Uri中使用 pack://siteoforigin:,并且当测试实例化此视图时,我收到有关无法解析文件的错误.

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.

XAML:

<ResourceDictionary
   xmlns="...">

   <ImageBrush 
      x:Key="ResourceName"
      ImageSource="pack://siteoforigin:,,,/Resources/image.png" 
      />
</ResourceDictionary>

错误消息:

 Could not find a part of the path 'C:\\Solution\\TestResults\\Workspace_2012-03-01 14_54_29\\Resources\\image.png'

我已经将Resources目录添加为部署项,并且已经确认该映像是TestRun输出目录.看来AppDomain正在操作我的测试程序集所在位置上方的一个文件夹,因为该文件实际上位于:

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 \ \ Resources \ image.png

c:\Solution\TestResults\Workspace_2012-03-01 14_54_29\ Out \Resources\image.png

关于如何使WPF应用程序将Out目录用作主文件夹的任何建议?

Any suggestions on how I can get the WPF Application to use the Out directory as it's primary folder?

推荐答案

这是因为测试运行程序设置的AppDomain.BaseDirectory没有结尾的'/'字符,这导致解析路径,将丢失路径中的最后一个目录.

This is because the AppDomain.BaseDirectory that is setup by your test runner doesn't have a trailing '/' character, this causes the code that resolves siteoforigin paths to lose the last directory in the path.

您可以通过在正常运行或测试中查看以下代码的结果来进行检查.

You can check this by looking at the result of the following code when running normally or in tests.

Console.WriteLine(AppDomain.CurrentDomain.BaseDirectory);

此问题最近在NUnit中(已包含)已已修复在2.6中),但对于其他测试跑步者可能仍然是个问题.

This has been recently fixed in NUnit (included in 2.6), but may still be an issue with other test runners.

如果您有兴趣,这等效于siteoforigin代码的作用:

If you're interested this is the equivalent of what the siteoforigin code is doing:

new Uri(new Uri(baseDirectory), "some/relative/path.jpg").LocalPath

尝试在baseDirectory中使用斜线结尾和不使用斜线结尾.

Try that with and without a trailing slash in baseDirectory.

这篇关于使用siteoforigin包Uri进行WPF应用程序的单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆