Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><code>HttpRequest</code> features a ton of non-virtual properties and methods. You're never going to be able to <em>mock</em> 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 <em>is</em> testable, we reduce the amount of manual testing we need to do. </p> <p>The best bet you have available to you is to create a class that you <em>can</em> mock (i.e. with virtual methods or sporting and cast as an interface) and only access the <code>HttpRequest</code> <em>through</em> that class. </p> <p>For example, if you were wanting to get certain cookie values as part of your page processing, you might create a <code>CookieHandler</code> class as follows:</p> <pre><code>public class CookieHandler { private HttpRequest CurrentRequest; public CookieHandler(HttpRequest request) { CurrentRequest = request; } public virtual HttpCookie UserLanguagePreference { get { return CurrentRequest.Cookies["UserLanguagePreference"].Name; } } } </code></pre> <p>This isn't an ideal class, it's just for example; because <code>UserLanguagePreference</code> is virtual, it can be mocked by Rhino. You might make <code>CookeHandler</code> 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. </p> <p>The point is that you can't test <em>everything</em> 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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload