Playing with aspx page cycle using JustMock

Posted by mehfuzh on ASP.net Weblogs See other posts from ASP.net Weblogs or by mehfuzh
Published on Thu, 29 Apr 2010 16:23:09 GMT Indexed on 2010/04/29 16:27 UTC
Read the original article Hit count: 505

Filed under:
|
|
|

In this post , I will cover 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 through ProcessRequest call, now if we take a look inside the method with reflector.net,  the call trace will go like : ProcessRequest –> ProcessRequestWithNoAssert –> SetInstrinsics –> Finallly ProcessRequest. Inside SetInstrinsics ,  it requires calls from HttpRequest, HttpResponse and HttpBrowserCababilities. Using this clue at hand, we can easily know the classes / calls  we need to mock in order to get through the expected call.

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

  1. var browser = Mock.Create<HttpBrowserCapabilities>();
  2. // Arrange
  3. Mock.Arrange(() => browser.PreferredRenderingMime).Returns("text/html");
  4. Mock.Arrange(() => browser.PreferredResponseEncoding).Returns("UTF-8");
  5. 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 +=delegate { pageSteps.Enqueue(PageStep.PreInit); };
  4. page.Load += delegate { pageSteps.Enqueue(PageStep.Load); };
  5. page.PreRender += delegate { pageSteps.Enqueue(PageStep.PreRender);};
  6. page.Unload +=delegate { pageSteps.Enqueue(PageStep.UnLoad);};
  7.  
  8. page.ProcessRequest(context);
  9.  
  10. Assert.True(requestContentEncodingSet);
  11. Assert.True(responseContentEncodingSet);
  12. Assert.True(rendered);
  13.  
  14. Assert.Equal(pageSteps.Dequeue(), PageStep.PreInit);
  15. Assert.Equal(pageSteps.Dequeue(), PageStep.Load);
  16. Assert.Equal(pageSteps.Dequeue(), PageStep.PreRender);
  17. Assert.Equal(pageSteps.Dequeue(), PageStep.UnLoad);
  18.  
  19. Mock.Assert(request);
  20. 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

Playing with aspx page cycle using JustMock

Posted on Dot net Slackers See other posts from Dot net Slackers
Published on Thu, 29 Apr 2010 00:00:00 GMT Indexed on 2010/04/29 16:47 UTC
Read the original article Hit count: 505

Filed under:
In this post , I will cover 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: public enum PageStep {     PreInit,     Load,     PreRender,     UnLoad } Once doing so, i  first...

Did you know that DotNetSlackers also publishes .net articles written by top known .net Authors? We already have over 80 articles in several categories including Silverlight. Take a look: here.



Email this Article

© Dot net Slackers or respective owner

Related posts about c#

Related posts about telerik