vendredi 9 janvier 2015

Unit testing WebRequest.Create static methods with mocks or else

I want to test my WebRequest implemented class to create a proxy applicaiton.


So I create a class that nemed ProxyService:



public class ProxyServive
{
private readonly IMyWebRequest proxyRequest;

public ProxyServive(IMyWebRequest proxyRequest)
{
this.proxyRequest = proxyRequest;
}

public WebResponse GetResponse(string url)
{
return proxyRequest.GetResponse(url);
}
}


The class that implemented IMyWebRequest interface is running WebRequest.Create() method. So I can not test this stuation.



public class MyWebRequest : IMyWebRequest
{
public WebResponse GetResponse(string url)
{
WebRequest request = WebRequest.Create(url);

using (WebResponse response = request.GetResponse())
{
return response;
}
}
}


If I send an invalid url to GetResponse() method, WebRequest.Create(url) method throws UriFormatException or WebException.


So I can not simulate this while testing. Is there any way?


Aucun commentaire:

Enregistrer un commentaire