天天看点

ASP.NET MVC实践系列10-单元测试

一、测试路由器:

我们首先利用mvc的模板创建一个解决方案,我们可以在Gloabal.asax.cs文件中发现如下代码

ASP.NET MVC实践系列10-单元测试

默认路由代码

ASP.NET MVC实践系列10-单元测试

测试代码

下面我们解释一下上面的代码

RouteCollection routes = new RouteCollection();

MvcApplication.RegisterRoutes(routes);

这两行的意思是将routes按照默认路由代码中的格式生成相应的路由。

var httpContextMock = new Mock<HttpContextBase>();

httpContextMock.Setup(c => c.Request.AppRelativeCurrentExecutionFilePath).Returns("~/Home/Index");

RouteData routeData = routes.GetRouteData(httpContextMock.Object);

这行的意思是从routes中获得路由数据,为下边断言路由中的内容作准备。

Assert.AreEqual("Home", routeData.Values["Controller"]);

Assert.AreEqual("Index", routeData.Values["action"]);

Assert.AreEqual("", routeData.Values["id"]);

这三行分别验证了路由中生成的Controller,action,id的正确性。

二、测试Controller与Action

1、测试ViewData

我们先来看一下ASP.NET MVC的默认模板中的测试代码:

ASP.NET MVC实践系列10-单元测试

Code

我们可以在测试代码中对viewData的内容进行验证,一般并不推荐使用弱类型的viewData来进行传递数据,下面我们来看一下如何来测试viewModel中的内容。

ASP.NET MVC实践系列10-单元测试

2、测试RedirectToAction:

ASP.NET MVC实践系列10-单元测试

三、测试View Helpers

我们常常会把一些牵扯到逻辑的View代码写到Helper中,所以测试Helper非常有意义,我们先写一个简单的Helper

ASP.NET MVC实践系列10-单元测试

Helper

这个Helper的完成的工作是将输入的集合数据分解然后按列表输出。

ASP.NET MVC实践系列10-单元测试

4-9行都是为实例化HtmlHelper做准备的。

四、参考

《Professional ASP.NET MVC 1.0》

《ProASP.NET MVCFramework》

<a href="http://msdn.microsoft.com/en-us/magazine/dd942838.aspx" target="_blank">http://msdn.microsoft.com/en-us/magazine/dd942838.aspx</a>

<a href="http://www.henrycordes.nl/post/2008/02/MVC-Framework-and-Unit-Test.aspx" target="_blank">http://www.henrycordes.nl/post/2008/02/MVC-Framework-and-Unit-Test.aspx</a>

<a href="http://srtsolutions.com/blogs/patricksteele/archive/2009/08/23/asp-net-mvc-mvc-contrib-unit-testing.aspx" target="_blank">http://srtsolutions.com/blogs/patricksteele/archive/2009/08/23/asp-net-mvc-mvc-contrib-unit-testing.aspx</a>

<a href="http://weblogs.asp.net/leftslipper/archive/2008/04/13/mvc-unit-testing-controller-actions-that-use-tempdata.aspx" target="_blank">http://weblogs.asp.net/leftslipper/archive/2008/04/13/mvc-unit-testing-controller-actions-that-use-tempdata.aspx</a>

我的ASP.NET MVC实践系列

<a href="http://www.cnblogs.com/nuaalfm/archive/2009/10/26/1589802.html" target="_blank">ASP.NET MVC实践系列1-UrlRouting</a>

<a href="http://www.cnblogs.com/nuaalfm/archive/2009/10/26/1590064.html" target="_blank">ASP.NET MVC实践系列2-简单应用</a>

<a href="http://www.cnblogs.com/nuaalfm/archive/2009/10/27/1590532.html" target="_blank">ASP.NET MVC实践系列3-服务器端数据验证</a>

<a href="http://www.cnblogs.com/nuaalfm/archive/2009/10/28/1591410.html" target="_blank">ASP.NET MVC实践系列4-Ajax应用</a>

<a href="http://www.cnblogs.com/nuaalfm/archive/2009/11/04/1595411.html" target="_blank">ASP.NET MVC实践系列5-结合jQuery</a>

<a href="http://www.cnblogs.com/nuaalfm/archive/2009/11/10/1596864.html" target="_blank">ASP.NET MVC实践系列6-Grid实现(上)</a>

<a href="http://www.cnblogs.com/nuaalfm/archive/2009/11/12/1601959.html" target="_blank">ASP.NET MVC实践系列7-Grid实现(下-利用Contrib实现)</a>

<a href="http://www.cnblogs.com/nuaalfm/archive/2009/11/18/1605364.html" target="_blank">ASP.NET MVC实践系列8-对查询后分页处理的解决方案</a>

<a href="http://www.cnblogs.com/nuaalfm/archive/2009/11/24/1609752.html" target="_blank">ASP.NET MVC实践系列9-filter原理与实践</a>

其他:

<a href="http://www.cnblogs.com/nuaalfm/archive/2009/11/11/1600811.html" target="_blank">在ASP.NET MVC中对表进行通用的增删改</a>

本文转自 你听海是不是在笑 博客园博客,原文链接:http://www.cnblogs.com/nuaalfm/archive/2009/11/26/1611420.html  ,如需转载请自行联系原作者