单元测试异步事件处理程序 [英] Unit Test async eventhandler

查看:173
本文介绍了单元测试异步事件处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[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?

推荐答案

有几种选择:

  1. Assert.AreEqual 您可以插入 Thread.sleep代码(MaxTimeoutForEvent)
  2. 使用延迟断言。一个例子可以在本<一个中找到href="http://stackoverflow.com/questions/14319435/delayed-nunit-assert-message-evaluation/27081445">question
  1. Before Assert.AreEqual you may insert Thread.Sleep(MaxTimeoutForEvent)
  2. Use delayed assert. An example can be found in this question

P.S。记得 TestCleanup :)

这篇关于单元测试异步事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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