I have been assigned to write unit tests for an application that I do not have the right to modify. The method I want to unit test makes a call like this:
HttpWebResponse response = await request.GetResponseAsync() as HttpWebResponse;
Obviously I do not want to actually make a request from my unit test - that would be an integration test. I have used Microsoft Fakes for thousands of other tests in this project, so I thought I would do the same here. The simplest and cleanest solution seems to me to shim the request.GetResponseAsync() method. Then I can return some fake content and ensure that the method handles it properly without actually making a request.
GetResponseAsync() returns a Task<WebResponse> object. So I would normally do something like:
using (ShimsContext.Create())
{
System.Net.Fakes.ShimWebRequest.AllInstances.GetResponseAsync = (x) =>
{
return new Task<WebResponse>(() =>
{
HttpWebResponse toRet = new HttpWebResponse();
return toRet;
});
}
}
The problem is that the above doesn't compile because
"'System.Net.HttpWebResponse.HttpWebResponse()' is obsolete: 'This API supports the .NET Framework infrastructure and is not intended to be used directly from your code.'"
I understand that the type is obsolete and we should be using something different now, but the legacy code I am trying to test does not allow me that luxury. I have looked at many questions on this topic, but none seem to answer this question.
Aucun commentaire:
Enregistrer un commentaire