【问题标题】:Unit Test async eventhandler单元测试异步事件处理程序
【发布时间】:2014-11-27 21:23:29
【问题描述】:
[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);
}

单元测试失败,因为方法是异步的。 字符串test 为“NULL”,因为值test2 稍后出现。我该如何解决这个问题?

【问题讨论】:

    标签: c# .net unit-testing


    【解决方案1】:

    有几种选择:

    1. Assert.AreEqual之前你可以插入Thread.Sleep(MaxTimeoutForEvent)
    2. 使用延迟断言。可以在question 中找到一个示例

    附:记住TestCleanup :)

    【讨论】:

    • 1.) Thread.Sleep -> 不起作用。 2.) 我无法解释代码,我如何在我的示例中使用它
    • 你确定async_eventhandler在TestMethod启动后被调用了吗?
    • 2.使用 NUnit:Assert.That("test_test", Is.EqualTo(test).After(3000));也不工作。 TestInit 和 Testmethod 开始同步。 testmethod 必须等待 testinit,但他们不会
    • [TestMethod] public void TestMethod1() { MessageBox.Show("2"); NUnit.Framework.Assert.That("test_test", Is.EqualTo(test).After(3000)); } //如果我使用 MessageBox.Show("2"),并等待单击消息框,直到事件 async_eventhandler 结束,然后得到正确的字符串测试测试通过 :) 但我不会使用 message.box :)
    • 我仍然没有正确答案。 Testmethod 必须等待通知事件。但我是怎么做到的?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多