Created
November 27, 2012 04:32
-
-
Save miso-soup/4152393 to your computer and use it in GitHub Desktop.
ASP.NET MVC のルーティングのテストです。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Web; | |
using System.Web.Mvc; | |
using System.Web.Routing; | |
using NUnit.Framework; | |
using Moq; | |
//テストコード | |
namespace RoutingTest | |
{ | |
[TestFixture] | |
public class RouteConfigTest | |
{ | |
/// <summary> | |
/// テスト準備のためのメソッドです。 | |
/// 登録したルーティングから、指定したURLに対応するルートデータを取得します。 | |
/// </summary> | |
/// <param name="url"></param> | |
/// <returns></returns> | |
private RouteData GetActualRouteData(string url) | |
{ | |
//ルーティングを登録 | |
RouteCollection routes = new RouteCollection(); | |
RouteConfig.RegisterRoutes(routes); | |
//指定したURLを返すよう、HttpContextのモックを作成します。 | |
var httpContextMock = new Mock<HttpContextBase>(); | |
httpContextMock.Setup(c => | |
c.Request.AppRelativeCurrentExecutionFilePath) | |
.Returns(url); | |
//モックを使い、指定したURLに対応するルートデータを取得します | |
RouteData routeData = routes.GetRouteData(httpContextMock.Object); | |
return routeData; | |
} | |
/// <summary> | |
/// 日記を表示するルーティングのテスト | |
/// </summary> | |
[Test] | |
public void Route_Diary_Test() | |
{ | |
//テストするURL | |
String url = "~/Person/Diary/2012/12/22"; | |
//結果予想値 | |
String expect_controller = "Person"; | |
String expect_action = "DiaryDetails"; | |
String expect_year = "2012"; | |
String epxect_month = "12"; | |
String expect_day = "22"; | |
//URLに対応するルートデータを取得 | |
RouteData actual = GetActualRouteData(url); | |
//assert | |
Assert.AreEqual(expect_controller, actual.Values["controller"]); | |
Assert.AreEqual(expect_action, actual.Values["action"]); | |
Assert.AreEqual(expect_year, actual.Values["year"]); | |
Assert.AreEqual(epxect_month, actual.Values["month"]); | |
Assert.AreEqual(expect_day, actual.Values["day"]); | |
} | |
} | |
} | |
//ルーティングの登録コード | |
namespace RoutingTest | |
{ | |
public class RouteConfig | |
{ | |
public static void RegisterRoutes(RouteCollection routes) | |
{ | |
routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); | |
routes.MapRoute( | |
name: "Diary", | |
url: "Person/Diary/{year}/{month}/{day}", | |
defaults: new { controller = "Person", action = "DiaryDetails", id = UrlParameter.Optional } | |
); | |
routes.MapRoute( | |
name: "Default", | |
url: "{controller}/{action}/{id}", | |
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } | |
); | |
} | |
} | |
} | |
//コントローラ | |
namespace RoutingTest.Controllers | |
{ | |
public class PersonController : Controller | |
{ | |
[HttpGet] | |
public ActionResult DiaryDetails(int year, int month, int day) | |
{ | |
return View(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
使用しているライブラリ
・Moq http://code.google.com/p/moq/
・Nunit http://www.nunit.org/