Assume we want to return a HttpResponseMessage from a controller with a string inside of "Content" like so:
return new HttpResponseMessage
{
Content = new StringContent("I like cats!")
};
Naturally, the string may be something a lot more useful than "I like cats!" as it could be a serialized type! How would we slurp this data back out in a unit test to affirm that it is what we expect. I don't know yet. I am able to get the "I like cats!" string back out into the cattyCopy variable below...
[TestMethod]
public void test_controller()
{
Whatever();
}
private async void Whatever()
{
var myModel = new MyModel();
var myController = new MyController();
HttpResponseMessage response = myController.Post(myModel);
Byte[] myBytes = await response.Content.ReadAsByteArrayAsync();
var cattyCopy = System.Text.Encoding.Default.GetString(myBytes);
}
But everything in the Whatever method happens asynchronously so there is no way to get an assertion inside to fail in a way that fails the test_controller method. The solution probably has to do with Tasks. I hardly ever play with async and await so I'm really green to it all.
No comments:
Post a Comment