Mocking HttpContext with JustMock

Posted by mehfuzh on ASP.net Weblogs See other posts from ASP.net Weblogs or by mehfuzh
Published on Thu, 29 Apr 2010 15:39:07 GMT Indexed on 2010/04/29 15:47 UTC
Read the original article Hit count: 439

Filed under:
|
|
|

In post , i will show a test code that will mock the various elements needed to complete a HTTP page request and  assert the expected page cycle steps. To begin, i have a simple enumeration that has my predefined page steps:

  1. public enum PageStep
  2. {
  3.     PreInit,
  4.     Load,
  5.     PreRender,
  6.     UnLoad
  7. }

Once doing so, i  first created the page object [not mocking].

  1. Page page = new Page();

Here, our target is to fire up the page process though ProcessRequest call, now if we take a look inside method though reflector, we will find calls stack like : ProcessRequest –> ProcessRequestWithNoAssert –> SetInstrinsics –> Finallly ProcessRequest. Inside SetIntrinsics , where it requires calls from HttpRequest, HttpResponse and HttpBrowserCababilities. With this , we can easily know what are classes / calls  we need to mock in order to get though the expected call.

Accordingly, for  HttpBrowserCapabilities our required test code will look like:

  1. Mock.Arrange(() => browser.PreferredRenderingMime).Returns("text/html");
  2. Mock.Arrange(() => browser.PreferredResponseEncoding).Returns("UTF-8");
  3. Mock.Arrange(() => browser.PreferredRequestEncoding).Returns("UTF-8");

Now, HttpBrowserCapabilities is get though [Instance]HttpRequest.Browser. Therefore, we create the HttpRequest mock:

  1. var request = Mock.Create<HttpRequest>();

Then , add the required get call :

  1. Mock.Arrange(() => request.Browser).Returns(browser);

As, [instance]Browser.PerferrredResponseEncoding and [instance]Browser.PreferredResponseEncoding  are also set to the request object and to make that they are set properly, we can add the following lines as well [not required though].

  1. bool requestContentEncodingSet = false;
  2. Mock.ArrangeSet(() => request.ContentEncoding = Encoding.GetEncoding("UTF-8")).DoInstead(() =>  requestContentEncodingSet = true);

Similarly, for response we can write:

  1.  var response = Mock.Create<HttpResponse>();
  2.  
  3.  bool responseContentEncodingSet = false;
  4.  Mock.ArrangeSet(() => response.ContentEncoding = Encoding.GetEncoding("UTF-8")).DoInstead(() => responseContentEncodingSet = true);

Finally , I created a mock of HttpContext and set the Request and Response properties that will returns the mocked version.

  1. var context = Mock.Create<HttpContext>();
  2.  
  3. Mock.Arrange(() => context.Request).Returns(request);
  4. Mock.Arrange(() => context.Response).Returns(response);

As, Page internally calls RenderControl method , we just need to replace that with our one and optionally we can check if  invoked properly:

  1. bool rendered = false;
  2. Mock.Arrange(() => page.RenderControl(Arg.Any<HtmlTextWriter>())).DoInstead(() => rendered = true);

That’s  it, the rest of the code is simple,  where  i asserted the page cycle with the PageSteps that i defined earlier:

  1. var pageSteps = new Queue<PageStep>();
  2.  
  3.  page.PreInit +=
  4.      delegate
  5.      {
  6.          pageSteps.Enqueue(PageStep.PreInit);
  7.      };
  8.  page.Load +=
  9.      delegate
  10.      {
  11.          pageSteps.Enqueue(PageStep.Load);
  12.      };
  13.  
  14.  page.PreRender +=
  15.      delegate
  16.      {
  17.          pageSteps.Enqueue(PageStep.PreRender);
  18.      };
  19.  
  20.  page.Unload +=
  21.      delegate
  22.      {
  23.          pageSteps.Enqueue(PageStep.UnLoad);
  24.      };
  25.  
  26.  page.ProcessRequest(context);
  27.  
  28.  Assert.True(requestContentEncodingSet);
  29.  Assert.True(responseContentEncodingSet);
  30.  Assert.True(rendered);
  31.  
  32.  Assert.Equal(pageSteps.Dequeue(), PageStep.PreInit);
  33.  Assert.Equal(pageSteps.Dequeue(), PageStep.Load);
  34.  Assert.Equal(pageSteps.Dequeue(), PageStep.PreRender);
  35.  Assert.Equal(pageSteps.Dequeue(), PageStep.UnLoad);
  36.  
  37.  Mock.Assert(request);
  38.  Mock.Assert(response);

 

You can get the test class shown in this post here to give a try by yourself with of course JustMock.

Enjoy!!

© ASP.net Weblogs or respective owner

Related posts about c#

Related posts about telerik