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. CentOS ASP.NET Core Runtime Jexus跨平台布署

    .net core 开源和跨平台,能布署到当前主流的Windows,Linux,macOS 系统上.本篇我们将在 Linux 系统上使用 ASP.NET Core Runtime 和 Jexus 布署 ...

  2. 构建NetCore应用框架之实战篇(三):BitAdminCore框架功能规划选择

    本篇承接上篇内容,如果你不小心点击进来,建议从第一篇开始完整阅读,文章内容继承性连贯性. 构建NetCore应用框架之实战篇系列 一.BitAdminCore功能规划 如何选择框架的落地功能,前篇文章 ...

  3. 终于,我们的新产品Fotor Slideshow Maker上线了!!

    辛苦了大半年,使用纯网页技术全新打造的首个交互式Slideshow产品终于上线了,现在是 http://slideshow.fotor.com,希望能尽快推出中文版! http://blog.foto ...

  4. 使用Dockerfile定制镜像

    Dockerfile是一个文本文件,其中包含额一条一条的指令,每一条指令构建一层,因此每一条指令的作用就是描述这一层应当如何的构建. 以构建nginx镜像为例,使用Dockerfile构建的步骤如下: ...

  5. 智能卡操作系统COS概述

    随着IC卡从简单的同步卡发展到异步卡,从简单的EPROM卡发展到内带微处理器的智能卡(又称CPU卡),对IC卡的各种要求越来越高.而卡本身所需要的各种管理工作也越来越复杂,因此就迫切地需要有一种工具来 ...

  6. 非对齐访问(unaligned accesses)

    从CPU角度看内存访问对齐 结构体成员非对齐访问所带来的思考 ARM体系中存储系统非对齐的存储访问操作 什么是cache line? cache line就是处理器从RAM load/store数据到 ...

  7. 使用BeanUitls提高对象拷贝效率

    首先来创建两个bean 注:一定要有set/get方法,成员变量必须要同名 public class User1 { String name; String password; String phon ...

  8. MySQL索引(六)

    一.什么是索引 索引就像一本书的目录一样,如果在字段上建立索引,那么以索引为列的查询条件时可以加快查询的速度,这个就是MySQL优化的重要部分 二.创建主键索引 整个表的每一条记录的主键值在表内都是唯 ...

  9. 【Java并发编程】:使用synchronized获取互斥锁

    在并发编程中,多线程同时并发访问的资源叫做临界资源,当多个线程同时访问对象并要求操作相同资源时,分割了原子操作就有可能出现数据的不一致或数据不完整的情况,为避免这种情况的发生,我们会采取同步机制,以确 ...

  10. rails 5.2 启动警告 warning: previous definition of VERSION was here和bootsnap

    bootsnap依赖问题 You should add gem 'bootsnap' to your gemfile to install it or remove the line require ...