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的中要做到身份认证微软为我们提供 ...
随机推荐
- Unity Shader 阶段性反思与总结(一)
Unity Shader 阶段性反思与总结(一) 最近在写Shader的时候,总是感觉力不从心,感觉自己已经看了蛮久的书了,也有一定的积累了,但是一想写什么效果,完完全全就是脑袋一团空白.典型的例子就 ...
- 使用vs code开发纸壳CMS并启用Razor智能提示
关于纸壳CMS 纸壳CMS是一个开源免费的,可视化设计,在线编辑的内容管理系统.基于ASP .Net Core开发,插件式设计: 下载代码 GitHub:https://github.com/Seri ...
- C#之使用AutoUpdater自动更新客户端
安装NuGet包 在Visio studio中右击解决方案,选择管理NuGet包,搜索安装Autoupdater.NET.Official. 工作简介 从服务器下载包含更新文件的XML文件,从中获取软 ...
- MSSQL数据表生成模型
MSSQL数据表生成模型 http://pan.baidu.com/s/1mhBAapy
- 深入浅出“跨视图粒度计算”--3、EXCLUDE表达式
本文由 网易云发布. 深入嵌入“跨视图粒度计算”的前面两篇分别讲了 1.理解数据的粒度 2.INCLUDE表达式 这一篇讲一下EXCLUDE表达式的用法. EXCLUDE,中文译为“排除”,顾名思义 ...
- Android Google Analytics
基础知识: 一个统计条目包含两类,一种是 screen,另一种是 event. Screen 包括 screen name. Event 包括 category,action,label 和 val ...
- Cordova - 彻底搞定安卓中的微信支付插件!
Cordova:8.0.0 Android studio:3.2.1 cordova-plugin-adam-wechat : 3.0.6 你看到这个标题肯定会惊讶,一个Cordova的微信支付插件, ...
- AGC032D Rotation Sort
题目传送门 Description 给定\(N\)的排列(\(N\leq5000\)),将任一区间最左侧的数插到该区间最右边的代价为\(A\),将任一区间最右侧的数插到该区间最左边的代价为\(B\), ...
- Mysql数据库二:表的增删改查
----建表CREATE TABLE emp( id int PRIMARY key auto_increment, name char(10) , birthday DATE , salary FL ...
- ZZNU 2076(退役学长最后的神功 zz题)
题目链接:http://acm.zznu.edu.cn/problem.php?pid=2076 输入一个T表示有T个样例每组实例一个整数n(0〈n〈1000接下来输入2*n个数字,代表一个2*n的矩 ...