xUnit and Moq do not support async - await keywords

女生的网名这么多〃 提交于 2019-12-05 00:06:00

Change your unit test method to return Task instead of void, and it should work. Support for async void unit tests is being considered for a future release.

I describe in detail why async unit tests don't work by default on my blog. (My blog examples use MSTest, but the same problems existed in every other test runner, including xUnit pre-1.9).

I tried to use the code from your 'Update', but it was stopping at the async method that I was mocking.

    var tcs = new TaskCompletionSource<T>();
    tcs.SetResult(default(T));


    mock.Setup(x => x.AnAsyncMethod(It.IsAny<T>())).Returns(tcs.Task);

So to fix that I had to change the 'Return' method:

    mock.Setup(x => x.AnAsyncMethod(It.IsAny<T>())).Returns(()=> { return tcs.Task; } );
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!