【问题标题】:Unit Testing WPF Application with siteoforigin pack Uri使用 siteoforigin 包 Uri 对 WPF 应用程序进行单元测试
【发布时间】:2012-03-01 20:10:31
【问题描述】:

我有一些我正在为 WPF 应用程序编写的单元测试,尽管我尽量避免它,但我有一些正在测试的代码实例化了一个视图。一旦实例化视图,就会评估所有标记扩展、样式等。为了解决这个问题,我创建了一个虚拟 Application 并在初始化测试程序集时注册了所有必需的资源:

[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);
       }
    }
}

我以前用过这种方法,效果还不错。

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

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 正在运行我的测试程序集位置上方的一个文件夹,因为该文件实际上位于:

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

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

【问题讨论】:

  • 手动强制应用程序域的基本文件夹是否有效? AppDomain.CurrentDomain.SetData("APPBASE", "FolderNameHere"); 似乎有更好的方法,但我的记忆力让我失望。这可能足够好(tm)。
  • 已经研究了好几个小时了。 AppDomain.CurrentDomain.BaseDirectory 返回 Out 文件夹。

标签: wpf unit-testing mstest vs-unit-testing-framework


【解决方案1】:

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

您可以通过查看以下代码在正常运行或测试时的结果来检查这一点。

Console.WriteLine(AppDomain.CurrentDomain.BaseDirectory);

这是最近在 NUnit 中的 fixed(包含在 2.6 中),但可能仍然是其他测试运行器的问题。

如果您有兴趣,这相当于 siteoforigin 代码正在执行的操作:

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

baseDirectory 中尝试使用或不使用斜杠。

【讨论】:

  • 是的,斜杠是罪魁祸首。我在发布后不久就发现了这一点,并且没有回来添加我的发现,但无论如何我都会接受你的回答。我实际上在 2010 年使用 MSTest,它也没有放入斜杠。
【解决方案2】:

answer above 中所述,我已通过反射更改AppDomain.CurrentDomain.BaseDirectory,成功解决了该问题:

private static void FixWpfPackSiteOfOriginReferences()
{
    // note: xunit does not add a trailing slash to the BaseDirectory path.
    // When you start the application, there's a trailing slash.
    // Also see:
    // - https://stackoverflow.com/a/9908449/684096
    // - https://github.com/dotnet/runtime/issues/7866#issuecomment-299761968

    var appDomainSetup = GetInstancePropertyValueViaReflection<AppDomainSetup>(
        AppDomain.CurrentDomain,
        "FusionStore");

    if (!appDomainSetup.ApplicationBase.EndsWith(
             Path.DirectorySeparatorChar.ToString()))
    {
        appDomainSetup.ApplicationBase += Path.DirectorySeparatorChar;
    }
}


private static T GetInstancePropertyValueViaReflection<T>(
     object instance,
     string propertyName)
{
    PropertyInfo property = GetInstancePropertyInfo(instance, propertyName);
    return (T)property.GetValue(instance);
}

private static PropertyInfo GetInstancePropertyInfo(
    object instance,
    string propertyName)
{
    Type instanceType = instance.GetType();
    PropertyInfo propertyInfo = instanceType
        .GetProperty(
            propertyName, 
            BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
    if (propertyInfo == null)
    {
        throw new ArgumentOutOfRangeException(
            nameof(propertyName),
            propertyName,
            FormattableString.Invariant(
                $"{instanceType} does not have a property '{propertyName}'"));
    }

    return propertyInfo;
}

这是在测试中创建 Wpf-App 之前完成的,在我的例子中,它本身的范围是 xunit collection fixture

【讨论】:

    猜你喜欢
    • 2021-05-27
    • 1970-01-01
    • 2012-03-21
    • 2011-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-01
    • 2016-04-05
    相关资源
    最近更新 更多