Mvc Moq HttpContext
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的更多相关文章
- [转]ASP.NET Core / MVC 6 HttpContext.Current
本文转自:http://www.spaprogrammer.com/2015/07/mvc-6-httpcontextcurrent.html As you know with MVC 6, Http ...
- ASP.NET MVC中HttpContext, HttpContextBase, HttpContextWrapper联系
ttpContext HttpContext是最原始的ASP.NET Context. MVC的目的之一是能够单元测试.HttpContext没有base class,并且不是virtual,所以不能 ...
- MVC中HttpContext, HttpContextBase, HttpContextWrapper联系
HttpContext // // 摘要: // 封装有关个别 HTTP 请求的所有 HTTP 特定的信息. public sealed class HttpContext : IServicePro ...
- ASP.Net请求处理机制初步探索之旅 - Part 5 ASP.Net MVC请求处理流程
好听的歌 我一直觉得看一篇文章再听一首好听的歌,真是种享受.于是,我在这里嵌入一首好听的歌,当然你觉得不想听的话可以点击停止,歌曲 from 王菲 <梦中人>: --> 开篇:上一篇 ...
- MVC和Webform的比较和替换总结
1.自定义控件,页面赋值可用HtmlHelper进行扩展 2.aspx的母版页可用Layout代替 3.webform的request,response方法在MVC中同样适应,只是类有点不同,例如表单 ...
- ASP.NET MVC:窗体身份验证及角色权限管理示例
ASP.NET MVC 建立 ASP.NET 基础之上,很多 ASP.NET 的特性(如窗体身份验证.成员资格)在 MVC 中可以直接使用.本文旨在提供可参考的代码,不会涉及这方面太多理论的知识. 本 ...
- MVC身份验证及权限管理
MVC自带的ActionFilter 在Asp.Net WebForm的中要做到身份认证微软为我们提供了三种方式,其中最常用的就是我们的Form认证,需要配置相应的信息.例如下面的配置信息: < ...
- ASP.Net MVC请求处理流程
ASP.Net MVC请求处理流程 好听的歌 我一直觉得看一篇文章再听一首好听的歌,真是种享受.于是,我在这里嵌入一首好听的歌,当然你觉得不想听的话可以点击停止,歌曲 from 王菲 <梦中人& ...
- MVC身份验证及权限管理(转载)
from https://www.cnblogs.com/asks/p/4372783.html MVC自带的ActionFilter 在Asp.Net WebForm的中要做到身份认证微软为我们提供 ...
随机推荐
- Sql语法高级应用之七:如何在存储过程中使用事务
普通事物: USE Wot_Inventory; GO BEGIN TRANSACTION tr; DECLARE @error INT; SET @error = 0; SELECT * FROM ...
- JavaWeb(HTML +css+js+Servlet....)
注意 1.不要把函数命名为add(),不然容易和自带的冲突报错 2.是createElement 不要把create中的e写掉了 3.记得是getElementsByTaxName和getElemen ...
- Python 面向对象编程的一些特征 及 单例模式的实现, 父类方法的调用(python2/python3)
一.面向对象编程几个特征(封装, 继承,多态) 1.封装:类里面封装函数和变量, 在将类进行实例化成实例对象时进行传参, 从而生成不同的实例对象,增加代码的复用. 2.继承:子类可以继承父类的方法和属 ...
- poj3070 Fibonacci(矩阵快速幂)
矩阵快速幂基本应用. 对于矩阵乘法与递推式之间的关系: 如:在斐波那契数列之中 f[i] = 1*f[i-1]+1*f[i-2] f[i-1] = 1*f[i-1] + 0*f[i-2].即 所以, ...
- 安装gitlab
安装源: git clone https://gitlab.com/gitlab-org/gitlab-ce.git -b 7-10-stable gitlab 安装文档: 在github上:gitl ...
- 基于jeesite+android开发 电子商务系统免费教程
下载地址: jeesite免费教程 基于jeesite+android开发 电子商务系统免费教程 基于jeesite+android开发 电子商务系统免费教程 这个教程已经录制完很久了,一直没有公开, ...
- JDBC连接数据库7个步骤
JDBC连接数据库 •创建一个以JDBC连接数据库的程序,包含7个步骤: 1.JDBC所需的四个参数(user,password,url,driverClass) (1)user用户名 ( ...
- QQ聊天框测试用例设计
QQ.微信聊天框的主要功能就是发送消息和接收别人发过来的消息. 消息内容类型: 纯文字 纯图片 纯表情 文字+表情 文件 发送键: 点击“发送”发送 使用快捷键发送(针对电脑端) 用户在线状态: 在线 ...
- POJ 1154
#include<iostream> #include<stdio.h> #define MAXN 20 using namespace std; int DFS(int i, ...
- 剑指offer三十九之平衡二叉树
一.题目 输入一棵二叉树,判断该二叉树是否是平衡二叉树. 二.思路 详解代码. 三.代码 public class Solution { //判断根节点左右子树的深度,高度差超过1,则不平衡 ...