Unit Testing Windows 8 Store App UI (Xaml Controls)

前端 未结 1 458
忘掉有多难
忘掉有多难 2021-01-12 05:02

I\'ve been creating a Windows Store App but I have thread problems testing a method which creates a Grid (Which is a XAML Control). I\'ve tried to test using NUnit and MSTe

1条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-12 05:22

    Your controls related code needs to be run on a UI thread. Try:

    [TestMethod]
    async public Task CreateThumbnail_EmptyLayout_ReturnsEmptyGrid()
    {
        int count = 0;
        await ExecuteOnUIThread(() =>
        {
            Layout l = new Layout();
            ThumbnailCreator creator = new ThumbnailCreator();
            Grid grid = creator.CreateThumbnail(l, 192, 120);
            count = grid.Children.Count;
        });
    
        Assert.AreEqual(count, 0);
    }
    
    public static IAsyncAction ExecuteOnUIThread(Windows.UI.Core.DispatchedHandler action)
    {
        return Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, action);
    }
    

    The above should work on MS Test. I don't know about NUnit.

    0 讨论(0)
提交回复
热议问题