Is there a way to unit test an async method?

亡梦爱人 提交于 2019-12-04 15:42:07

问题


I am using Xunit and NMock on .NET platform. I am testing a presentation model where a method is asynchronous. The method creates an async task and executes it so the method returns immediately and the state I need to check aren't ready yet.

I can set a flag upon finish without modifying the SUT but that would mean I would have to keep checking the flag in a while loop for example, with perhaps timeout.

What are my options?


回答1:


Does your object feature any sort of signal that the asynchronous method is finished, such as an event? If that is the case, you can use the following approach:

[Test]
public void CanTestAsync()
{
    MyObject instance = new MyObject()
    AutoResetEvent waitHandle = new AutoResetEvent(false); 
    // create and attach event handler for the "Finished" event
    EventHandler eventHandler = delegate(object sender, EventArgs e) 
    {
        waitHandle.Set();  // signal that the finished event was raised
    } 
    instance.AsyncMethodFinished += eventHandler;

    // call the async method
    instance.CallAsyncMethod();

    // Wait until the event handler is invoked
    if (!waitHandle.WaitOne(5000, false))  
    {  
        Assert.Fail("Test timed out.");  
    }  
    instance.AsyncMethodFinished -= eventHandler;    
    Assert.AreEqual("expected", instance.ValueToCheck);
}



回答2:


Just thought you might want an update on this since the #1 answer is actually recommending an older pattern to solve this problem.

In .net 4.5 + xUnit 1.9 or higher you can simply return a Task and optionally use the async keyword from your test to have xunit wait for the test to complete asynchronously.

See this article on xUnit.net 1.9

[Fact]
public async Task MyAsyncUnitTest()
{    
  // ... setup code here ...     
  var result = await CallMyAsyncApi(...);     
  // ... assertions here ...
}



回答3:


My preferred method is to mock out and inject the actual threading mechanism so that under test it is not asynchronous. Some times that is not possible (if the threading of the method is part of the framework, or otherwise not under your control).

If you can't control thread creation, then waiting for the thread to finish in some way, either a while loop or just a timed wait for however long the thread is supposed to take and failing the test if the state is not there since it took too long anyway.




回答4:


check out my article on unit testing Silverlight applications

http://www.codeproject.com/KB/silverlight/Ag3DemoLOB.aspx

There is an example of unit testing a method that calls a WCF service asynchronously...



来源:https://stackoverflow.com/questions/1174702/is-there-a-way-to-unit-test-an-async-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!