1: public class MockMvcHttpContext

   2: {

   3:     public Moq.Mock<System.Web.HttpContextBase> Context { get; set; }

   4:  

   5:     public Moq.Mock<System.Web.HttpRequestBase> Request { get; set; }

   6:  

   7:     public Moq.Mock<System.Web.HttpResponseBase> Response { get; set; }

   8:  

   9:     public System.Web.HttpCookieCollection Cookie { get; set; }

  10:  

  11:     public System.Web.Mvc.Controller Controller { get; set; }

  12:  

  13:     private System.Collections.Specialized.NameValueCollection _form;

  14:     public System.Collections.Specialized.NameValueCollection Form {

  15:         get

  16:         {

  17:             return _form;

  18:         }

  19:         set

  20:         {

  21:             _form = value;

  22:             this.Controller.ValueProvider = new System.Web.Mvc.FormCollection(value).ToValueProvider();

  23:         }

  24:     }

  25:     public System.Collections.Specialized.NameValueCollection Query { get; set; }

  26: }

  27:  

  28: public static class MvcTestExtensions

  29: {

  30:     public static MockMvcHttpContext SetupFullContext(this System.Web.Mvc.Controller controller)

  31:     {

  32:         return controller.SetupContext().SetupRequest().SetupResponse().SetupCookie().SetupForm().SetupQuerystring();

  33:     }

  34:  

  35:     public static MockMvcHttpContext SetupContext(this System.Web.Mvc.Controller controller)

  36:     {

  37:         

  38:         var context = new Moq.Mock<System.Web.HttpContextBase>();

  39:         controller.ControllerContext = new System.Web.Mvc.ControllerContext(context.Object, new System.Web.Routing.RouteData(), controller);

  40:         var obj = new MockMvcHttpContext()

  41:         {

  42:             Controller = controller,

  43:             Context=context

  44:         };

  45:         return obj;

  46:     }

  47:  

  48:     public static MockMvcHttpContext SetupRequest(this MockMvcHttpContext context)

  49:     {

  50:         if (context.Context == null)

  51:         {

  52:             throw new System.ArgumentNullException("context.Context");

  53:         }

  54:         var request = new Moq.Mock<System.Web.HttpRequestBase>();

  55:         context.Context.SetupGet(x => x.Request).Returns(request.Object);

  56:         context.Request = request;

  57:         return context;

  58:     }

  59:  

  60:     public static MockMvcHttpContext SetupResponse(this MockMvcHttpContext context)

  61:     {

  62:         if (context.Context == null)

  63:         {

  64:             throw new System.ArgumentNullException("context.Context");

  65:         }

  66:         var response = new Moq.Mock<System.Web.HttpResponseBase>();

  67:         context.Context.SetupGet(x => x.Response).Returns(response.Object);

  68:         context.Response = response;

  69:         return context;

  70:     }

  71:  

  72:     public static MockMvcHttpContext SetupCookie(this MockMvcHttpContext context)

  73:     {

  74:         if (context.Context == null)

  75:         {

  76:             throw new System.ArgumentNullException("context.Context");

  77:         }

  78:         var cookie = new System.Web.HttpCookieCollection();

  79:         if (context.Response != null)

  80:         {

  81:             context.Response.SetupGet(x => x.Cookies).Returns(cookie);

  82:         }

  83:         if (context.Request != null)

  84:         {

  85:             context.Request.SetupGet(x => x.Cookies).Returns(cookie);

  86:         }

  87:         context.Cookie = cookie;

  88:         

  89:         return context;

  90:     }

  91:  

  92:     public static MockMvcHttpContext SetupForm(this MockMvcHttpContext context)

  93:     {

  94:         if (context.Context == null)

  95:         {

  96:             throw new System.ArgumentNullException("context.Context");

  97:         }

  98:         

  99:         if (context.Request == null)

 100:         {

 101:             throw new System.ArgumentNullException("context.Request");

 102:         }

 103:         var form = new System.Collections.Specialized.NameValueCollection();

 104:         context.Request.SetupGet(x => x.Form).Returns(form);

 105:         context.Form = form;

 106:         return context;

 107:     }

 108:  

 109:     public static MockMvcHttpContext SetupQuerystring(this MockMvcHttpContext context)

 110:     {

 111:         if (context.Context == null)

 112:         {

 113:             throw new System.ArgumentNullException("context.Context");

 114:         }

 115:  

 116:         if (context.Request == null)

 117:         {

 118:             throw new System.ArgumentNullException("context.Request");

 119:         }

 120:         var query = new System.Collections.Specialized.NameValueCollection();

 121:         context.Request.SetupGet(x => x.QueryString).Returns(query);

 122:         context.Query = query;

 123:         return context;

 124:     }

 125: }

需要引入Moq库

 

useage:

controller.SetupFullContext()

Mvc Moq HttpContext的更多相关文章

  1. [转]ASP.NET Core / MVC 6 HttpContext.Current

    本文转自:http://www.spaprogrammer.com/2015/07/mvc-6-httpcontextcurrent.html As you know with MVC 6, Http ...

  2. ASP.NET MVC中HttpContext, HttpContextBase, HttpContextWrapper联系

    ttpContext HttpContext是最原始的ASP.NET Context. MVC的目的之一是能够单元测试.HttpContext没有base class,并且不是virtual,所以不能 ...

  3. MVC中HttpContext, HttpContextBase, HttpContextWrapper联系

    HttpContext // // 摘要: // 封装有关个别 HTTP 请求的所有 HTTP 特定的信息. public sealed class HttpContext : IServicePro ...

  4. ASP.Net请求处理机制初步探索之旅 - Part 5 ASP.Net MVC请求处理流程

    好听的歌 我一直觉得看一篇文章再听一首好听的歌,真是种享受.于是,我在这里嵌入一首好听的歌,当然你觉得不想听的话可以点击停止,歌曲 from 王菲 <梦中人>: --> 开篇:上一篇 ...

  5. MVC和Webform的比较和替换总结

    1.自定义控件,页面赋值可用HtmlHelper进行扩展 2.aspx的母版页可用Layout代替 3.webform的request,response方法在MVC中同样适应,只是类有点不同,例如表单 ...

  6. ASP.NET MVC:窗体身份验证及角色权限管理示例

    ASP.NET MVC 建立 ASP.NET 基础之上,很多 ASP.NET 的特性(如窗体身份验证.成员资格)在 MVC 中可以直接使用.本文旨在提供可参考的代码,不会涉及这方面太多理论的知识. 本 ...

  7. MVC身份验证及权限管理

    MVC自带的ActionFilter 在Asp.Net WebForm的中要做到身份认证微软为我们提供了三种方式,其中最常用的就是我们的Form认证,需要配置相应的信息.例如下面的配置信息: < ...

  8. ASP.Net MVC请求处理流程

    ASP.Net MVC请求处理流程 好听的歌 我一直觉得看一篇文章再听一首好听的歌,真是种享受.于是,我在这里嵌入一首好听的歌,当然你觉得不想听的话可以点击停止,歌曲 from 王菲 <梦中人& ...

  9. MVC身份验证及权限管理(转载)

    from https://www.cnblogs.com/asks/p/4372783.html MVC自带的ActionFilter 在Asp.Net WebForm的中要做到身份认证微软为我们提供 ...

随机推荐

  1. C#文件监控对象FileSystemWatcher实例,通过监控文件创建、修改、删除、重命名对服务器数据进行实时备份

    先上图,简单的windorm界面:此为最初的版本,后续会增加监听多个源目录的功能.log功能.进度条展示功能等. 1.初始化监听 /// <summary> /// 初始化监听 /// & ...

  2. javascript 对象克隆

    浅克隆 先看代码: /** * 浅克隆 克隆传入对象,只克隆一层 * @param {any} source */ function shallowClone(source) { var tiaget ...

  3. 程序媛计划——python正则表达式

    #定义 正则表达式是对字符串操作的一种逻辑公式,通过它我们能筛选过滤出我们需要的内容,如判断一串数字是否是电话号码. #原理 先把正则表达式的字符串转换成 Pattern 对象,接着用这个对象处理文本 ...

  4. CF特征码遍历

    HOOK_游戏代码 8B 00 8B 08 8B 91 A8 00 00 00 地址-15 4E5E954E5EA 44E5E95DIRECT 从733E00开始搜 6B 00 94 51 6C 地址 ...

  5. 蒲公英(bzoj2724)(分块+区间众数)

    Input Output Sample Input 6 3 1 2 3 2 1 2 1 5 3 6 1 5 Sample Output 1 2 1 HINT \(n <= 40000\),$ m ...

  6. django 自定义中间件 middleware

    Django 中间件 Django中的中间件是一个轻量级.底层的插件系统,可以介入Django的请求和响应处理过程,修改Django的输入或输出.中间件的设计为开发者提供了一种无侵入式的开发方式,增强 ...

  7. 异步加载的JS如何在chrome浏览器断点调试?

    我们常常利用chrome强大的控制台Sources下面进行代码断点调试,但是通过$.getScript等异步加载JS的方式在Sources里面就是找不到,那如何进行debug断点调试呢? 方案一: 在 ...

  8. #阿里云#云服务器部署可道云(KodExplorer)

    前言:在做一些项目的时候,经常有一些文档交流,修改之后的文档在QQ或微信上发来发去,还要下载,很是不爽,有一个挺有用的东西叫做KodExplorer可道云. kodexplorer可道云是目前国内有代 ...

  9. javascript数据结构与算法--二叉树遍历(后序)

    javascript数据结构与算法--二叉树遍历(后序) 后序遍历先访问叶子节点,从左子树到右子树,再到根节点. /* *二叉树中,相对较小的值保存在左节点上,较大的值保存在右节点中 * * * */ ...

  10. eclipseGUI的可视化开发工具插件

    一   各种GUI开发插件的特色 Eclipse并不自带GUI的可视化开发工具,那么如果要在Eclipse进行可视化的GUI开发,就需要依靠第三方的插件. 1. Visual Editor Eclip ...