如何使用HttpWebRequest / Response依赖项对方法进行单元测试

时间:2014-09-09 13:36:55

标签: unit-testing httpwebrequest moq httpwebresponse

尝试对此方法进行单元测试,但无法弄清楚如何操作。

public bool ValidateCaptcha(string captchaResponse, string captchaChallenge,
                            string hostip)
    {
        var strPrivateKey = _secConfiguration.CaptchaPrivateKey;
            var strParameters = "verify?privatekey=" + strPrivateKey +
                "&remoteip=" + hostip +
                "&challenge=" + captchaChallenge+
                "&response=" + captchaResponse;

            var url = CaptchaUrl + strParameters;
            var request = CreateHttpWebRequest(url);
            request.Proxy.Credentials = CredentialCache.DefaultCredentials;
            request.Method = "POST";
            request.ContentType = "text/html";
            request.ContentLength = 0;

            var response = GetHttpWebResponse(request);                
            var writer = response.GetResponseStream();                
            var reader = new StreamReader(writer);
            var responseFromServer = reader.ReadToEnd();

            var serverResponse = responseFromServer.Split('\n');
            return serverResponse[0] == "true";
    }

    private HttpWebResponse GetHttpWebResponse(HttpWebRequest request)
    {
        return (HttpWebResponse) request.GetResponse();
    }

    private HttpWebRequest CreateHttpWebRequest(string url)
    {
        return (HttpWebRequest) WebRequest.Create(url);
    }

我曾计划对依赖项进行moq并有几个包装类

public class WrapHttpWebRequest : IHttpWebRequest
{
    private readonly HttpWebRequest _request;

    public WrapHttpWebRequest(HttpWebRequest request)
    {
        _request = request;
    }

    public string Method
    {
        get { return _request.Method; }
        set { _request.Method = value; }
    }

    public IHttpWebResponse GetResponse()
    {
        return new WrapHttpWebResponse((HttpWebResponse)_request.GetResponse());
    }

}

public class WrapHttpWebResponse : IHttpWebResponse
{
    private WebResponse _response;

    public WrapHttpWebResponse(HttpWebResponse response)
    {
        _response = response;
    }

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    private void Dispose(bool disposing)
    {
        if (disposing)
        {
            if (_response != null)
            {
                ((IDisposable)_response).Dispose();
                _response = null;
            }
        }
    }

    public Stream GetResponseStream()
    {
        return _response.GetResponseStream();
    }
}

但无法找到注入它们的方法。任何想法我怎么能这样做?感谢。

1 个答案:

答案 0 :(得分:0)

您可以在测试中创建类的可测试版本。 在可测试版本中使方法虚拟覆盖

protected virtual HttpWebRequest CreateHttpWebRequest(string url)
{
    return (HttpWebRequest)WebRequest.Create(url);
}

public class TesableClassUnderTest : ClassUnderTest
{
    public HttpWebRequest HttpWebRequestFake { get; set; }

    protected override HttpWebRequest CreateHttpWebRequest(string url)
    {
        if (HttpWebRequestFake != null)
            return HttpWebRequestFake;
        return base.CreateHttpWebRequest(url);
    }
} 

然后在测试中将假对象设置为您自己的值:

[TestMethod]
public void test()
{
    TesableClassUnderTest cut = new TesableClassUnderTest();
    cut.HttpWebRequestFake = CreateFakeHttpWebRequest();
    cut.ValidateCaptcha(...)
    Assert...
}