天天看點

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  ,如需轉載請自行聯系原作者

繼續閱讀