单元测试的Windows 8 Store应用UI(XAML控件) [英] Unit Testing Windows 8 Store App UI (Xaml Controls)
本文介绍了单元测试的Windows 8 Store应用UI(XAML控件)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经创建一个Windows Store应用,但我有问题线程测试它创建了一个网格(这是一个XAML控制)的方法。
我已经试图测试使用NUnit和MSTest的。
测试方法是:
[TestMethod的]
酒店的公共空间CreateThumbnail_EmptyLayout_ReturnsEmptyGrid()
{
布局L =新的布局();
ThumbnailCreator创造者=新ThumbnailCreator();
格之格= creator.CreateThumbnail(L,192,120);
诠释计数= grid.Children.Count;
Assert.AreEqual(计数,0);
}
而creator.CreateThumbnail(这引发错误的方法):
公共电网CreateThumbnail(布局升,双totalWidth,双totalHeight)
{
电网newGrid =新的网格( );
newGrid.Width = totalWidth;
newGrid.Height = totalHeight;
的SolidColorBrush backGroundBrush =新的SolidColorBrush(BackgroundColor中);
newGrid.Background = backGroundBrush;
newGrid.Tag = 1;
返回newGrid;
}
当我运行这个测试,它抛出这个错误:
System.Exception的:应用程序调用被编组为不同的线程的接口。 (从HRESULT异常:0x8001010E(RPC_E_WRONG_THREAD))
解决方案
您控制相关的代码需要在UI线程上运行。尝试:
[TestMethod的]
异步公共任务CreateThumbnail_EmptyLayout_ReturnsEmptyGrid()
{
诠释计数= 0;
等待ExecuteOnUIThread(()=>
{
布局L =新的布局();
ThumbnailCreator创造者=新ThumbnailCreator();
格之格=创造者。 CreateThumbnail(L,192,120);
计数= grid.Children.Count;
});
Assert.AreEqual(计数,0);
}
公共静态IAsyncAction ExecuteOnUIThread(Windows.UI.Core.DispatchedHandler动作)
{
返回Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher .RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal,动作);
}
以上应MS测试工作。我不知道NUnit的。
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 MSTest.
The test method is:
[TestMethod]
public void CreateThumbnail_EmptyLayout_ReturnsEmptyGrid()
{
Layout l = new Layout();
ThumbnailCreator creator = new ThumbnailCreator();
Grid grid = creator.CreateThumbnail(l, 192, 120);
int count = grid.Children.Count;
Assert.AreEqual(count, 0);
}
And the creator.CreateThumbnail (The method which throws the error):
public Grid CreateThumbnail(Layout l, double totalWidth, double totalHeight)
{
Grid newGrid = new Grid();
newGrid.Width = totalWidth;
newGrid.Height = totalHeight;
SolidColorBrush backGroundBrush = new SolidColorBrush(BackgroundColor);
newGrid.Background = backGroundBrush;
newGrid.Tag = l;
return newGrid;
}
When I run this test it throws this error:
System.Exception: The application called an interface that was marshalled for a different thread. (Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD))
解决方案
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.
这篇关于单元测试的Windows 8 Store应用UI(XAML控件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文