Search code examples
c#windowsxamlwindows-store

Windows Store App Unit test fails when using custom control


I have a Windows Store App (C# + XAML) and wanted to create some unit tests for it. I created the unit test project for this within my solution and the default test method works fine. Then I added my project as reference to the unit test project and the tests stopped working:

------ 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) ==========

I googled a lot and found a thread that explained the error could be within the App.xaml and indeed I could track it down to this 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"/>

If I change it to:

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

the tests run fine but I do no longer have a search highlight on that text block.

How can I have both - the search highlights and running unit tests within my solution?


Solution

  • This might be related to https://connect.microsoft.com/VisualStudio/feedback/details/790477/winrt-mstest-runner-fails-when-using-ilist-t-properties-of-custom-types-from-xaml

    As far as I can tell, use of some custom types in Xaml is incompatible with the WinRT unit test runner, because that does not provide proper IXamlType interface implementations in some cases.

    So it's possible that there's something about these attachable properties that you're using that causes the test runner to provider incorrect metadata for them. (Without being able to see your HighLightString class it's hard to be sure. But you can verify whether this is the case by turning on Mixed mode debugging for your unit test project, and debugging the tests. Configure Visual Studio to break on all CLR exceptions when Thrown (and not just when unhandled), and if you eventually hit a NotImplementedException (possibly after a few other exceptions) in a type called RunTimeXamlSystemBaseType then this is the problem you're running into.)

    I've worked around this in the past by avoiding putting the relevant types in my App.xaml. You only run into this problem if you attempt to load the relevant Xaml. (Not much help if you actually need these things to be at global scope of course.)