HttpRequest
features a ton of non-virtual properties and methods. You're never going to be able to mock it. If you're accessing it from Page.Request, you can't even instantiate a new one and set its values for your test. This is one of the reasons that the UI rarely (if ever) gets automated tests; it's frequently just not possible. That's why we use patterns that allow automated testing such as MVC and MVVM: by pushing as much processing logic as we can to a separate class that is testable, we reduce the amount of manual testing we need to do.
The best bet you have available to you is to create a class that you can mock (i.e. with virtual methods or sporting and cast as an interface) and only access the HttpRequest
through that class.
For example, if you were wanting to get certain cookie values as part of your page processing, you might create a CookieHandler
class as follows:
public class CookieHandler
{
private HttpRequest CurrentRequest;
public CookieHandler(HttpRequest request)
{
CurrentRequest = request;
}
public virtual HttpCookie UserLanguagePreference
{
get
{
return CurrentRequest.Cookies["UserLanguagePreference"].Name;
}
}
}
This isn't an ideal class, it's just for example; because UserLanguagePreference
is virtual, it can be mocked by Rhino. You might make CookeHandler
a singleton (replaceable in your tests), you might use property-based injection on your UI Page, or you might use a DI framework like Unity.
The point is that you can't test everything with automated unit tests, so don't bother. Test as much as you can and save the rest for integration and manual user acceptance tests.