Mocking HttpRequest in ASP.NET 4.0

匆匆过客 提交于 2019-12-03 16:16:42

The HttpRequestBase and HttpRequestWrapper classes can be used with a bit of work.

Wherever you currently access HttpContext.Current.Request -- or just plain Page.Request -- you'll need to use an injectable instance of HttpRequestBase instead. Then you'll need to inject a different subclass of HttpRequestBase depending on whether you're testing or live.

  • For live code, you'd probably inject an HttpRequestWrapper instance that wraps HttpContext.Current.Request:

    var liveRequest = new HttpRequestWrapper(HttpContext.Current.Request);
    
  • For test code, you'd need to create and inject your own mock subclass of HttpRequestBase. Presumably Moq can do that for you on-the-fly.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!