在Windows 8 Consumer Preview中使用UI对象进行单元测试 [英] Unit Testing with UI objects in Windows 8 Consumer Preview

查看:75
本文介绍了在Windows 8 Consumer Preview中使用UI对象进行单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用VS11/MSTest单元测试集成来测试Windows 8 Metro风格的类库.在一组测试中,我希望创建一个从FrameworkElement派生的模拟对象.不幸的是,您不能在非UI上创建任何此类UI元素 线程.有谁知道如何通过CoreDispatcher运行uint测试(据我所知,您只能从现有的UI对象访问此测试),或者如何在单元测试中创建FrameworkElement子类? /p>

谢谢

  安德鲁

更多信息:

我要进行单元测试的场景就是这样.我有一种方法可以接受FrameworkElement作为参数并根据一组规则更改DataContext进行测试.因此,我的测试类似于

 [TestMethod]
公共无效MyTest()
{
    MyService服务=新的MyService();
    MockElement元素=新的MockElement();
    
    service.SetDataContext(element);
    Assert.AreEqual(element.DataContext,...);
}
公共类MockElement:FrameworkElement
{
    ...
} 

由于测试未在UI线程上运行,因此尝试创建新的MockElement实例时出现异常.

解决方案


安德鲁,

您是否可以尝试将与UI相关的动作移动到其他线程(使用UI线程分派器).如果需要等待UI线程,则可以使用Async测试方法.

请确保从同一线程中捕获UI线程的异常. < o:p></o:p>

谢谢

布瓦



I am currently using the VS11/MSTest unit testing integration to test a Windows 8 Metro-style class library. In one set of tests I wish to create a mock object that derives from FrameworkElement. Unfortunately you cannot create any such UI elements on non-UI threads. Does anyone have any idea how to either run uint-tests via a CoreDispatcher (as far as I can tell you can only access this from an existing UI object), or to be able to create FrameworkElement sub-classes in a unit test?

Thanks,

    Andrew

Futher information:

The scenario I am trying to unit test is as such. I have a method to test that accepts a FrameworkElement as a parameter and alters the DataContext based upon a set of rules. My test therefore looks similar to,

[TestMethod]
public void MyTest()
{
    MyService service = new MyService();
    MockElement element = new MockElement();
    
    service.SetDataContext(element);
    Assert.AreEqual(element.DataContext, ...);
}
public class MockElement : FrameworkElement
{
    ...
}

Since the test does not run on the UI thread, I get an exception when trying to create a new MockElement instance.

解决方案


Hi Andrew,

Can you try moving the UI related actions to a different thread ( using UI Thread dispatcher). You can use Async test methods if you need to wait on the UI thread.

Make sure to catch the exceptions of the UI thread from within the same thread. <o:p></o:p>

Thanks

Bhuva



这篇关于在Windows 8 Consumer Preview中使用UI对象进行单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆