How to Mock HttpWebRequest and HttpWebResponse using the Moq Framework?

Posted by Nicholas on Stack Overflow See other posts from Stack Overflow or by Nicholas
Published on 2010-05-11T09:17:07Z Indexed on 2010/05/11 9:24 UTC
Read the original article Hit count: 2401

Filed under:
|
|

How do you use the Moq Framework to Mock HttpWebRequest and HttpWebResponse in the following Unit Test?

[Test]
public void Verify_That_SiteMap_Urls_Are_Reachable()
{
    // Arrange - simplified
    Uri uri = new Uri("http://www.google.com");

    // Act
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();

    // Asset
    Assert.AreEqual("OK", response.StatusCode.ToString());
}

© Stack Overflow or respective owner

Related posts about moq

Related posts about unit-testing