Sunday, May 10, 2015

how to unit test the HttpResponseMessage "Content" handed back from an ASP.NET Web API controller

I found the async/await answer for the challenge I detail here and it looks like so:

[TestMethod]
public void test_controller()
{
   var task = Whatever();
   task.Wait();
   string stuff = task.Result;
   Assert.AreEqual(stuff,"I like cats!");
}
 
private async Task Whatever()
{
   var myModel = new MyModel();
   var myController = new MyController();
   HttpResponseMessage response = myController.Post(myModel);
   Byte[] myBytes = await response.Content.ReadAsByteArrayAsync();
   return System.Text.Encoding.Default.GetString(myBytes);
}

 
 

This is how one sniffs out a type. The type does not have to be a string.

No comments:

Post a Comment