【问题标题】:Windows Store App Unit test fails when using custom control使用自定义控件时 Windows 应用商店应用单元测试失败
【发布时间】:2013-05-08 12:49:17
【问题描述】:

我有一个 Windows 应用商店应用程序 (C# + XAML),并想为它创建一些单元测试。 我在我的解决方案中为此创建了单元测试项目,默认测试方法工作正常。 然后我将我的项目添加为对单元测试项目的引用,并且测试停止工作:

------ Discover test started ------
========== Discover test finished: 1 found (0:00:00,8748072) ==========
------ Run test started ------
Updating the layout...

Copying files: Total 2 mb to layout...

Registering the application to run from layout...

Deployment complete. Full package name: "58d19822-a649-46ba-b3fd-36c60b2709d7_1.0.0.0_neutral__t4zwj4xd20b1w"

Failed to activate Windows Store app unit test executor. Error: The remote procedure call failed.
========== Run test finished: 0 run (0:00:05,0596667) ==========

我用谷歌搜索了很多,发现一个解释错误的线程可能在 App.xaml 中,实际上我可以追踪到这个 TextBlock:

<TextBlock Grid.ColumnSpan="2" controls:HighLightString.FullText="{Binding Path=FullName}" controls:HighLightString.SelectedText="{Binding DataContext.QueryText, ElementName=resultsPanel}" controls:HighLightString.FgColor="{StaticResource SAPHighlightColor}"  Style="{StaticResource TileTitleTextStyle}" Margin="20,0,0,0" TextTrimming="WordEllipsis"/>

如果我将其更改为:

<TextBlock Grid.ColumnSpan="2" Text="{Binding Path=FullName}" Style="{StaticResource TileTitleTextStyle}" Margin="20,0,0,0" TextTrimming="WordEllipsis"/>

测试运行良好,但该文本块上不再有搜索突出显示。

我怎样才能同时拥有 - 搜索突出显示和在我的解决方案中运行单元测试?

【问题讨论】:

  • 所以,我实际上在我的项目中遇到了同样的问题。您是否有任何其他具有相同属性名称的附加属性?似乎是“全文”。
  • 我通过删除 app.xaml 中的文本块定义并将其放入搜索结果页面 xaml 定义中解决了我的问题。这样 app.xaml 就“干净”了,并且可以执行单元测试。

标签: c# windows xaml windows-store


【解决方案1】:

这可能与https://connect.microsoft.com/VisualStudio/feedback/details/790477/winrt-mstest-runner-fails-when-using-ilist-t-properties-of-custom-types-from-xaml有关

据我所知,在 Xaml 中使用某些自定义类型与 WinRT 单元测试运行程序不兼容,因为在某些情况下它不能提供正确的 IXamlType interface 实现。

因此,您使用的这些可附加属性可能会导致测试运行程序为它们提供不正确的元数据。 (无法看到您的 HighLightString 类,很难确定。但您可以通过为您的单元测试项目打开混合模式调试并调试测试来验证是否是这种情况。配置 Visual Studio 以中断所有抛出时的 CLR 异常(而不仅仅是未处理时),并且如果您最终在称为 RunTimeXamlSystemBaseType 的类型中遇到了 NotImplementedException(可能是在其他一些异常之后),那么这就是您遇到的问题。)

我过去曾通过避免将相关类型放入我的App.xaml 来解决此问题。如果您尝试加载相关的 Xaml,您只会遇到此问题。 (当然,如果您确实需要将这些东西放在全球范围内,那么帮助不大。)

【讨论】:

  • 今晚我遇到了这个问题,因为在事物之间共享的样式引用了测试运行者不喜欢的 something。我已经部分缩小了范围,但解决方法很痛苦......
【解决方案2】:

我遇到了完全相同的问题。
我在列表框的 DataTemplate 中使用
Microsft Prism for Windows 8.1Adventure Works Shopping Example 的 HighlightOnErrors 行为。

<interactivity:Interaction.Behaviors>
                <behaviors:HighlightOnErrors PropertyErrors="{Binding Errors[Value], Mode=TwoWay}" 
                                                         OriginalStyleName="VariationTableItemTextBoxStyle" 
                                                         HighlightStyleName="HighlightVariationTableItemTextBoxStyle"/>
            </interactivity:Interaction.Behaviors>

行为正常,单元测试运行正常,但在列表框项模板中没有这种行为。

【讨论】:

    【解决方案3】:

    在此处的 Microsoft Connect 站点上为此提交了一个错误:https://connect.microsoft.com/VisualStudio/feedback/details/848688/unit-testing-windows-store-app-fails-when-style-contains-custom-attached-dependency-properties

    列出的解决方法是引用应用的构建 .exe,而不是使用项目引用。我可以确认该解决方法确实有效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-07
      • 2012-10-10
      • 1970-01-01
      相关资源
      最近更新 更多