Created
July 23, 2014 11:55
-
-
Save cpoDesign/159bd95cae0a28dd1c2b to your computer and use it in GitHub Desktop.
Custom route testing in MVC 5.
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
public class RouteConfig | |
{ | |
public static void RegisterRoutes(RouteCollection routes) | |
{ | |
routes.MapRoute( | |
name: "BlogArticle", | |
url: "{controller}/{year}/{month}/{name}", | |
defaults: new { controller = "blog", action = "Index", year = UrlParameter.Optional, month = UrlParameter.Optional, name = UrlParameter.Optional } | |
); | |
} | |
} |
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
[TestClass] | |
public class RoutingTests | |
{ | |
/// <summary> | |
/// unit testing custom route | |
/// </summary> | |
[TestMethod] | |
public void TestingCustomRouteToBindBlogArticle() | |
{ | |
var context = new StubHttpContextForRouting(requestUrl: "~/blog/2014/01/Powershell"); | |
var routes = new RouteCollection(); | |
RouteConfig.RegisterRoutes(routes); | |
// Act | |
RouteData routeData = routes.GetRouteData(context); | |
// Assert | |
Assert.IsNotNull(routeData); | |
Assert.AreEqual("blog", routeData.Values["controller"]); | |
Assert.AreEqual("Index", routeData.Values["action"]); | |
Assert.AreEqual("2014", routeData.Values["year"]); | |
Assert.AreEqual("01", routeData.Values["month"]); | |
Assert.AreEqual("Powershell", routeData.Values["name"]); | |
} | |
} | |
/// Source for this helper class: http://bradwilson.typepad.com/blog/2010/07/testing-routing-and-url-generation-in-aspnet-mvc.html | |
public class StubHttpContextForRouting : HttpContextBase | |
{ | |
StubHttpRequestForRouting _request; | |
StubHttpResponseForRouting _response; | |
public StubHttpContextForRouting(string appPath = "/", string requestUrl = "~/") | |
{ | |
_request = new StubHttpRequestForRouting(appPath, requestUrl); | |
_response = new StubHttpResponseForRouting(); | |
} | |
public override HttpRequestBase Request | |
{ | |
get { return _request; } | |
} | |
public override HttpResponseBase Response | |
{ | |
get { return _response; } | |
} | |
} | |
public class StubHttpRequestForRouting : HttpRequestBase | |
{ | |
string _appPath; | |
string _requestUrl; | |
public StubHttpRequestForRouting(string appPath, string requestUrl) | |
{ | |
_appPath = appPath; | |
_requestUrl = requestUrl; | |
} | |
public override string ApplicationPath | |
{ | |
get { return _appPath; } | |
} | |
public override string AppRelativeCurrentExecutionFilePath | |
{ | |
get { return _requestUrl; } | |
} | |
public override string PathInfo | |
{ | |
get { return ""; } | |
} | |
public override NameValueCollection ServerVariables | |
{ | |
get { return new NameValueCollection(); } | |
} | |
} | |
public class StubHttpResponseForRouting : HttpResponseBase | |
{ | |
public override string ApplyAppPathModifier(string virtualPath) | |
{ | |
return virtualPath; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment