Any idea on Mocking HttpRequest ?

Topics: Web Client Software Factory, User Forum
Feb 22, 2007 at 6:28 PM
Well I realize that HttpRequest can not be mocked on account of it being sealed. I need to write tests where I need to supply my applications some input which comes typically from httprequest (http headers, quesy string , form data etc).

I was thinking of one approach I could take would be this :
1- Create interfaces IMyHttpRequest and IMyHttpResponse and there implementations MyHttpRequest and MyHttpRespons. These implementations just calll HttpRequest and HttpResponse methods.
2- Use MyHttpRequest instead of HttpRequest in my application.
3- Create MockHttpRequest implementing IMyHttpRequest.

If any one think there is another better way to do it please do share.

Regards & thanks
Feb 22, 2007 at 7:44 PM
You could, instead, write two methods: one method that takes/uses the HttpRequest and only extracts the parameters needed for the second method. The second method, which takes either simple types or types you control can be unit tested as much as you want. The first method will need to be very carefully inspected and the test team you work with will need to know about the lack of unit tests there so they can focus on it a bit more. And if the first method takes a request and returns a response, do the same thing for the response. Have the first method take the return value/values from the method you have unit tested and use them to set properties on the response object, but keep the logic minimal. There were a few spots in the evolution of the Composite Web AB that we used this technique pretty effectively.

This goes back to why those of us who use TDD (which is a horrible name, look for rants on this subject elsewhere) like patterns like the View-Presenter. Since the pain involved in unit testing the view is too high, you make the code as simple and stupid as possible. Then you unit test the heck out of the Presenter. You review and inspect the pass throughs in the View for problems with someone else, and you have other tests that pound on the View itself.
Feb 22, 2007 at 8:11 PM
Thanks Michael. Definitely a good alternative.