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:
Once doing so, i first created the page object [not mocking].
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:
Now, HttpBrowserCapabilities is get through [Instance]HttpRequest.Browser. Therefore, we create the HttpRequest mock:
Then , add the required get call :
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].
Similarly, for response we can write:
Finally , I created a mock of HttpContext and set the Request and Response properties that will returns the mocked version.
As, Page internally calls RenderControl method , we just need to replace that with our one and optionally we can check if invoked properly:
That’s it, the rest of the code is simple, where i asserted the page cycle with the PageSteps that i defined earlier:
You can get the test class shown in this post here to give a try by yourself with of course JustMock :-).
Subscribe to be the first to get our expert-written articles and tutorials for developers!