简体   繁体   English

UWP单元测试无法访问应用程序资源

[英]UWP Unit Testing cannot access Application Resources

I'm pretty new to UWP and I need to unit test a service located in the UI project of the application. 我对UWP还是很陌生,我需要对应用程序UI项目中的服务进行单元测试。 This service gets host name and port settings from the App.xaml file of the UI project. 此服务从UI项目的App.xaml文件获取主机名和端口设置。 When I run the unit test for this service, the unit test application environment has trouble accessing the Application Resources dictionary (I copied the host name and port settings to the xaml file of the UnitTest project). 当我为此服务运行单元测试时,单元测试应用程序环境无法访问“应用程序资源”字典(我将主机名和端口设置复制到UnitTest项目的xaml文件中)。

Any ideas ? 有任何想法吗 ?

Hard to tell without seeing your actual code, but have you tried running you unit test code in the UI thread? 在看不到实际代码的情况下很难说出来,但是您是否尝试过在UI线程中运行单元测试代码? This works for me when I'm testing UWP code reading .resw resource files. 当我测试UWP代码读取.resw资源文件时,这对我有用。 Here is a code snippet using xUnit in UWP unit test project: 这是在UWP单元测试项目中使用xUnit的代码片段:

    [Fact]
    public async void ShouldPass()
    {
        await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(
            Windows.UI.Core.CoreDispatcherPriority.Normal, () => 
            {
                // --> your unit test code goes here <--
            });
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM