2

I have an async Task unit test (MVC, c#, .NET 4.5.2). It does an await on a aysnc Task<ActionResult> method, which in turn has an await call on a async method.

The test, and others like it, will pass if I select them and choose Debug Selected Tests from the right-click menu in Visual Studio 2017.

The problem is when I select Run Selected Tests or Run All. It is then that many of the tests will fail if they follow the condition mentioned at the beginning. Any test that only returns a RedirectToRouteResult without having gone the aforementioned drill-down will pass.

[TestMethod]
public async Task TestPartsController_GetPartInfo_ReturnsInfo()
{
   //arrange
   PartController pc = new PartController();

   //act
   var result = await pc.GetPartInfo("PC123456");

   //assert
   Assert.IsIntanceOfType(result, typeof(ViewResult));
   Assert.AreEqual("Form", ((ViewResult)result).ViewName);
   Assert.AreEqual("PC123456", result.Model.PartNum.ToUpper());
}

public async Task<ActionResult> GetPartInfo(string partNum)
{
   if (string.IsNullOrEmpty(partNum)
   {
      return RedirectToAction("Index")
   }

   var response = await ServiceClient.GetJsonAsync("/part/partinfo", "?partNum=" + partNum;
   response.EnsureSuccessStatusCode();
   results = await response.Content.ReadAsAsync<Dto.PartNumInfo>();
   ...
   return View("Form", model);
}

public async Task<HttpResponseMessage> GetAsync(Controllers controller, string criteria)
{
   HttpClient client;
   string service = GetService(controller, out client);
   var response = await client.GetAsync(service + criteria);
   return response;
}

Solution Use async/await all the way through as well as using statements and IDisposable.

 public async Task<HttpResponseMessage> GetJsonAsync<T>(Controllers controller, T data)
 {
    HttpResponseMessage response;

    using (var service = new MyService())
    {
       HttpClient http;
       string serviceLoc = service.GetServiceClient(controller, out http);
       response = await http.GetAsync(serviceLoc, data);
    }

    return response;
}
3
  • 2
    It is then that many of the tests will fail Can you provide some details about how they fail? Also, I have a hard time figuring out the meaning of if they follow the condition mentioned at the beginning. What does the condition refer to? Commented May 22, 2017 at 19:55
  • They fail by not being able to continue drilling down into the async methods. The condition is what is mentioned in the first paragraph. Commented May 23, 2017 at 10:36
  • You should post your solution as an answer, not an edit to the question. You can then accept the answer. Commented May 23, 2017 at 21:48

1 Answer 1

1

Solution Use async/await all the way through as well as using statements and IDisposable.

public async Task<HttpResponseMessage> GetJsonAsync<T>(Controllers controller, T data)
{
    HttpResponseMessage response;

    using (var service = new MyService())
    {
       HttpClient http;
       string serviceLoc = service.GetServiceClient(controller, out http);
       response = await http.GetAsync(serviceLoc, data);
    }

    return response;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.