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的中要做到身份认证微软为我们提供 ...
随机推荐
- C#——调用C++的DLL 数据类型转换
/C++中的DLL函数原型为 //extern "C" __declspec(dllexport) bool 方法名一(const char* 变量名1, unsig ...
- 如何连接MSSQLSERVER2014(其他版本数据库大体相同)
购买了服务器后我在上面安装了SqlServer2014,但是程序中总是远程连接不上,特此记录一下: 一.一些基本设置如下链接: 转载自:https://blog.sun0816.com/20381.h ...
- SQL Server2008 R2 数据库镜像实施手册(双机)
一.配置主备机 1. 服务器基本信息 主机名称为:HOST_A,IP地址为:192.168.1.155 备机名称为:HOST_B,IP地址为:192.168.1.156 二.主备实例互通 实现互通可以 ...
- OCP 12c 062题库大更新,出现大量新题-5
5.One of your databases supports an OLTP workload. The default undo tablespace is fixed size with: 1 ...
- 本机的虚拟机执行ifconfig,显示不出ip的解决方法
源于:https://blog.csdn.net/fuweihua123/article/details/78423715?locationNum=4&fps=1 本机的虚拟机执行ifconf ...
- C# 5.0-.Net新特性
调用者信息特性 CallerMemberNameAttribute | CallerFilePathAttribute | CallerLineNumberAttribute .NET Framewo ...
- Java抽象工厂模式
Java抽象工厂模式 基本定义 抽象工厂模式是所有形态的工厂模式中最为抽象和最其一般性的.抽象工厂模式可以向客户端提供一个接口,使得客户端在不必指定产品的具体类型的情况下,能够创建多个产品族的产品对象 ...
- ubuntu sudo: pip:找不到命令
编辑文件 /etc/sudoers sudo vi /etc/sudoers 将Defaults env_reset ,改为 Defaults !env_reset 编辑文件-/.bashers ...
- rabbitmq系列一 之简单队列
1. rabbitmq简介 rabbitmq是一个消息代理,或者讲是一个消息中间件.主要是用来接收和转发信息的,它是对消息不做任何处理的.MQ是消费-生产者模型的一个典型的代表,一端往消息队列中不断写 ...
- PullToRefreshListView 进入界面不能立即显示刷新控件的bug 解决方案
1.首先定位到PullToRefreshListView.java这个文件 2.找到onRefreshing(final boolean doScroll)方法 @Override protected ...