被测试Action 包含有Url对象的代码:

                 data = new data
{
title = ds.Name,
icon = "folder",
attr = new { id = "root", href = Url.Action("ManageDSByLevel", new { levelid = firstlevel.ID }), name = ds.Name },
},

在进行单元测试时需要Mock Url对象,以顺利通过测试,以下为Mock Url 代码:

             var dsController = new DealerStructureController(dsService.Object, pcService.Object);
dsController.Mapper = mapper.Object; var context = new Mock<HttpContextBase>();
var request = new Mock<HttpRequestBase>();
var response = new Mock<HttpResponseBase>();
//var session = new Mock<HttpSessionStateBase>();
//var server = new Mock<HttpServerUtilityBase>(); context.Setup(ctx => ctx.Request).Returns(request.Object);
context.Setup(ctx => ctx.Response).Returns(response.Object);
//context.Setup(ctx => ctx.Session).Returns(session.Object);
//context.Setup(ctx => ctx.Server).Returns(server.Object); //request.SetupGet(x => x.ApplicationPath).Returns("/");
//request.SetupGet(x => x.Url).Returns(new Uri("http://localhost/a", UriKind.Absolute));
//request.SetupGet(x => x.ServerVariables).Returns(new NameValueCollection()); //response.Setup(x => x.ApplyAppPathModifier(It.IsAny<string>())).Returns<string>(x => x); //context.SetupGet(x => x.Request).Returns(request.Object);
//context.SetupGet(x => x.Response).Returns(response.Object); var routes = new RouteCollection();
MvcApplication.RegisterRoutes(routes);
var helper = new UrlHelper(new RequestContext(context.Object, new RouteData()), routes);
dsController.Url = helper;

在这段代码中 Url对象使用了Url.Action方法,所以在mock Url 时只是简单的模拟,如果代码中使用了Urlde其他方法,可把注解的代码恢复使用。

Mvc controller单元测试 Mock Url对象的更多相关文章

  1. Mvc Controller 单元测试 Mock User.Identity.Name

    被测试的Action 包含 User.Identity.Name 代码,在写测试代码需要Mock ControllerContext对象 代码如下: var mock = new Mock<Co ...

  2. Spring MVC Controller 单元测试

    简介 Controller层的单元测试可以使得应用的可靠性得到提升,虽然这使得开发的时间有所增加,有得必失,这里我认为得到的比失去的多很多. Sping MVC3.2版本之后的单元测试方法有所变化,随 ...

  3. 在Spring MVC Controller中注入HttpServletRequest对象会不会造成线程安全的问题

    做法: 1.比如我们在Controller的方法中,通常是直接将HttpServletRequest做为参数,而为了方便节省代码,通常会定义为全局变量,然后使用@Autowire注入. 说明: 1.观 ...

  4. ZendFramework-2.4 源代码 - 关于MVC - Controller层

    // 1.控制器管理器 class ServiceManager implements ServiceLocatorInterface { public function __construct(Co ...

  5. .net Mvc Controller 接收 Json/post方式 数组 字典 类型 复杂对象

    原文地址:http://www.cnblogs.com/fannyatg/archive/2012/04/16/2451611.html ------------------------------- ...

  6. ASP.NET MVC和ASP.NET Core MVC中获取当前URL/Controller/Action (转载)

    ASP.NET MVC 一.获取URL(ASP.NET通用): [1]获取完整url(协议名+域名+虚拟目录名+文件名+参数) string url=Request.Url.ToString(); [ ...

  7. 基于spring与mockito单元测试Mock对象注入

    转载:http://www.blogjava.net/qileilove/archive/2014/03/07/410713.html 1.关键词 单元测试.spring.mockito 2.概述 单 ...

  8. 关于Spring MVC Controller 层的单元测试

    关于Spring MVC Controller 层的单元测试 测试准备工作: 1.搭建测试Web环境 2.注入Controller 类 3.编写测试数据 测试数据的文件名一定要与测试类的文件名相同,比 ...

  9. mvc+webapi 单元测试

    1.前言 现在这个项目已经有阶段性的模块完成了,所以就想着对这些模块进行单元测试,以保证项目的代码的质量.首先虽然标题是mvc+webapi实质上我只是对mvc进行的测试.用的时候vs的unit te ...

随机推荐

  1. Spring AOP配置与应用

    1.     两种方式: a)     使用Annotation b)     使用xml 2.     Annotation a)     加上对应的xsd文件spring-aop.xsd b)   ...

  2. 用python正则表达式提取字符串

    在日常工作中经常遇见在文本中提取特定位置字符串的需求.python的正则性能好,很适合做这类字符串的提取,这里讲一下提取的技巧,正则表达式的基础知识就不说了,有兴趣的可以看re的教程. 提取一般分两种 ...

  3. eclipse GWT开发环境的离线布置方法

    安装方法http://blog.csdn.net/u011029071/article/details/10143841 用eclipse自动更新安装失败N次,还是得手动来 以Google Plugi ...

  4. jQuery 实现上下,左右滑动

    前几天的任务:http://t.sina.com.cn/  的下滑效果. 渐变移动出足够的空白 -> 淡出最后一个 ->渐变移动出足够的空白 我们要做的是向左移动效果.这个效果用时需添加一 ...

  5. struts 2学习笔记—初学struts 2

    首先我学习了struts 1.x与struts 2的区别: 1.struts 1.x的控制器类必须从Action类继承. 2.struts 2的控制器类可以是一个普通的类,也可以是ActionSupp ...

  6. 《Linux Device Drivers》第十章 中断处理——note

    概述:系统要及时的感知硬件的状态,通常有两种方式:一种是轮询.一种是通过响应硬件中断.前者会浪费处理器的时间,而后者不会. 准备并口 在没有节设定产生中断之前,并口是不会产生中断的 并口的标准规定设置 ...

  7. iOS UIKit:viewController之Segues (4)

    @import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...

  8. NP-难题

    所谓NP-难题,在给定的一个信息系统中,假设研究对象书目为m,属性书目为n,则要考察的属性集P的一个子集是否为最小子集,要进行n*m*m次的比较.而n个属性可构成2的n次方个子集,这些子集都有可能是最 ...

  9. HTML设置固定页脚飘浮

    Css /* 页脚 */.footSty{bottom: 0pt; margin: 0pt; position: fixed; width: 100%; z-index: 10 ! important ...

  10. CSS Clip剪切元素实例

    一.实例1: .fixed { position: fixed; width: 382px; height: 100px; background: red; border: 1px solid blu ...