Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This is not a perfect solution yet it worked for me before and deserves extra care for the simplicity :</p> <p><a href="http://haacked.com/archive/2007/06/19/unit-tests-web-code-without-a-web-server-using-httpsimulator.aspx" rel="nofollow noreferrer">HTTPSimulator</a></p> <p>Also a typemock example documented in <a href="http://www.typemock.com/community/viewtopic.php?p=2744&amp;highlight=httprequest" rel="nofollow noreferrer">typemock forums</a>:</p> <pre><code>using System; using System.IO; using System.Net; using NUnit.Framework; using TypeMock; namespace MockHttpWebRequest { public class LibraryClass { public string GetGoogleHomePage() { HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.google.com"); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); using (StreamReader reader = new StreamReader(response.GetResponseStream())) { return reader.ReadToEnd(); } } } [TestFixture] [VerifyMocks] public class UnitTests { private Stream responseStream = null; private const string ExpectedResponseContent = "Content from mocked response."; [SetUp] public void SetUp() { System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding(); byte[] contentAsBytes = encoding.GetBytes(ExpectedResponseContent); this.responseStream = new MemoryStream(); this.responseStream.Write(contentAsBytes, 0, contentAsBytes.Length); this.responseStream.Position = 0; } [TearDown] public void TearDown() { if (responseStream != null) { responseStream.Dispose(); responseStream = null; } } [Test(Description = "Mocks a web request using natural mocks.")] public void NaturalMocks() { HttpWebRequest mockRequest = RecorderManager.CreateMockedObject&lt;HttpWebRequest&gt;(Constructor.Mocked); HttpWebResponse mockResponse = RecorderManager.CreateMockedObject&lt;HttpWebResponse&gt;(Constructor.Mocked); using (RecordExpectations recorder = RecorderManager.StartRecording()) { WebRequest.Create("http://www.google.com"); recorder.CheckArguments(); recorder.Return(mockRequest); mockRequest.GetResponse(); recorder.Return(mockResponse); mockResponse.GetResponseStream(); recorder.Return(this.responseStream); } LibraryClass testObject = new LibraryClass(); string result = testObject.GetGoogleHomePage(); Assert.AreEqual(ExpectedResponseContent, result); } [Test(Description = "Mocks a web request using reflective mocks.")] public void ReflectiveMocks() { Mock&lt;HttpWebRequest&gt; mockRequest = MockManager.Mock&lt;HttpWebRequest&gt;(Constructor.Mocked); MockObject&lt;HttpWebResponse&gt; mockResponse = MockManager.MockObject&lt;HttpWebResponse&gt;(Constructor.Mocked); mockResponse.ExpectAndReturn("GetResponseStream", this.responseStream); mockRequest.ExpectAndReturn("GetResponse", mockResponse.Object); LibraryClass testObject = new LibraryClass(); string result = testObject.GetGoogleHomePage(); Assert.AreEqual(ExpectedResponseContent, result); } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload