[英]Unit Testing async void MVC Controller methods don't run
我正在嘗試測試userController,以通過其服務使用戶異步,從而使測試方法無效。 事實是,所有內容都可以編譯,但是測試無法運行。
[TestClass]
public class UserControllerTests
{
[TestMethod]
public async void UserController_GetAll_Returns_Not_Null()
{
var result = await controller.GetALL();
Assert.IsNotNull(result);
}
}
我在“亞歷山大的嵌入式Techwire”此處的鏈接中找到了答案,只是更改了異步任務的異步無效性。
“異步無效”方法是“即發即棄”的方法,它們可能永遠不會回來,並且在需要從方法中收集結果和斷言的測試環境中,這根本沒有意義。
[TestClass]
public class UserControllerTests
{
[TestMethod]
public async Task UserController_GetAll_Returns_Not_Null()
{
var result = await controller.GetALL();
Assert.IsNotNull(result);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.