Unit Testing Async Methods
On a recent project I came across a situation where I needed to unit test a library that used RestSharp asynchronously. Here's an example of how it's done:
[Test]
public async Task GetBuildsOKTest()
{
var builds = new BuildsDTO();
var rr = new RestResponse<BuildsDTO>
{
StatusCode = HttpStatusCode.OK,
Data = builds
};
var restClient = new Mock<IRestClient>();
restClient
.Setup(x => x.ExecuteTaskAsync<BuildsDTO>(
It.IsAny<IRestRequest>(),
It.IsAny<CancellationToken>()))
.Returns(Task.FromResult(rr as IRestResponse<BuildsDTO>));
var _service = new VSTSService(restClient.Object);
var b = await _service.GetBuilds(BuildId);
Assert.IsNotNull(b);
}