本文介绍了单元测试异步事件处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
[TestInitialize]
public void Initialize()
{
//DO Something
async_eventhandler += the_eventhandler(async_eventhandler);
}
private void async_eventhandler
{
test = test2
// test2 comes from client, after here TestMethod have to start
}
[TestMethod]
public void TestMethod1()
{
Assert.AreEqual("test_test", test);
}
单元测试失败,因为该方法是异步。字符串测试
是空,因为值测试2
后话。我该如何解决这个问题呢?
The Unit Test failed, because the methods are async.String test
is "NULL" because the value test2
comes later. How can I solve the problem?
推荐答案
有几种选择:
- 在
Assert.AreEqual
您可以插入Thread.sleep代码(MaxTimeoutForEvent)
- 使用延迟断言。一个例子可以在本question
- Before
Assert.AreEqual
you may insertThread.Sleep(MaxTimeoutForEvent)
- Use delayed assert. An example can be found in this question
P.S。记得 TestCleanup
:)
这篇关于单元测试异步事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!