单元测试与任务℃的异步方法;布尔&GT;返回类型 [英] Unit testing an async method with Task<bool> return type
问题描述
我需要为下面的类的 InvokeAsync 方法单元测试。什么它只是做的是调用同一个类,其中包括复杂的逻辑分支和Web服务调用的私有方法。但是,单元测试只针对公共方法写的。所以,我应该在这种情况下怎么办?我应该在这里考的?任何建议将不胜AP preciated。
公共类MyCustomHandler
{
私人只读ILogger _logger;
私人只读HttpClient的_httpClient; 公共MyCustomHandler(HttpClient的客户端,ILogger记录仪)
{
_httpClient = HttpClient的;
_logger =记录仪;
} 公共覆盖异步任务&LT;布尔&GT; InvokeAsync()
{
返回等待InvokeReplyPathAsync();
} 私人异步任务&LT;布尔&GT; InvokeReplyPathAsync()
{
// code复杂的逻辑分支的地段和调用Web服务。
}
}
如果您的测试框架支持它(MSTest的做),你可以宣布你的测试方法异步
并调用从那里方法。我使用的是模拟框架,如犀牛嘲笑嘲笑的Web服务,所以你不需要依赖于实际的Web服务。
公共接口IWebService
{
任务&LT;布尔&GT; GetDataAsync();
}[识别TestClass]
公共类AsyncTests
{
[测试方法]
公共异步无效测试()
{
VAR的WebService = MockRepository.GenerateStub&LT; IWebService&GT;();
webService.Expect(X =&GT; x.GetDataAsync()),收益(新任务&LT;布尔&GT;(()=&GT; FALSE)); VAR将myHandler =新MyCustomHandler(web服务);
布尔结果=等待myHandler.InvokeAsync();
Assert.IsFalse(结果);
}
}[测试方法]
公共异步无效TestWebServiceException()
{
VAR的WebService = MockRepository.GenerateStub&LT; IWebService&GT;();
webService.Expect(X =&GT; x.GetDataAsync())。抛出(新引发WebException(服务不可用)); VAR将myHandler =新MyCustomHandler(web服务);
布尔结果=等待myHandler.InvokeAsync();
Assert.IsFalse(结果);
}
I need to create a unit test for the following class's InvokeAsync method. What it merely does is calling a private method in the same class which includes complex logical branches and web service calls. But the unit test are written only for the public methods. So what should I do in this scenario? What should I test in here? Any suggestions would be greatly appreciated.
public class MyCustomHandler
{
private readonly ILogger _logger;
private readonly HttpClient _httpClient;
public MyCustomHandler(HttpClient client, ILogger logger)
{
_httpClient = httpClient;
_logger = logger;
}
public override async Task<bool> InvokeAsync()
{
return await InvokeReplyPathAsync();
}
private async Task<bool> InvokeReplyPathAsync()
{
// Lot of code with complex logical branches and calling web services.
}
}
If your testing framework supports it (MsTest does) you can declare your test method async
and call the method from there. I'd mock the web services using a mock framework such as Rhino Mocks so you don't need to depend on the actual web service.
public interface IWebService
{
Task<bool> GetDataAsync();
}
[TestClass]
public class AsyncTests
{
[TestMethod]
public async void Test()
{
var webService = MockRepository.GenerateStub<IWebService>();
webService.Expect(x => x.GetDataAsync()).Return(new Task<bool>(() => false));
var myHandler = new MyCustomHandler(webService);
bool result = await myHandler.InvokeAsync();
Assert.IsFalse(result);
}
}
[TestMethod]
public async void TestWebServiceException()
{
var webService = MockRepository.GenerateStub<IWebService>();
webService.Expect(x => x.GetDataAsync()).Throw(new WebException("Service unavailable"));
var myHandler = new MyCustomHandler(webService);
bool result = await myHandler.InvokeAsync();
Assert.IsFalse(result);
}
这篇关于单元测试与任务℃的异步方法;布尔&GT;返回类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!