????4????????????URL????????????11-3URLTestDemo????У???Global.asax.cs???????RegisterRoutes??????????£?

 

public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute("MyRoute"?? "{controller}/{action}");
}

???????????RegisterRoutes??????????????????Create Unit Tests?????????????????????????????URLTestDemo.UnitTests?в???????????

 

MvcApplicationTest.cs??????????????????MvcApplicationTest???е???????????????????£?
namespace URLTestDemo.UnitTests
{
[TestClass()]
public class MvcApplicationTest
{
private HttpContextBase CreateHttpContext(string targetUrl=null??
string httpMethod = "GET")
{
Mock<HttpRequestBase> mockRequest = new Mock<HttpRequestBase>();
mockRequest.Setup(m => m.AppRelativeCurrentExecutionFilePath).Returns(targetUrl);
mockRequest.Setup(m => m.HttpMethod).Returns(httpMethod);
Mock<HttpResponseBase> mockResponse = new Mock<HttpResponseBase>();
mockResponse.Setup(m => m.ApplyAppPathModifier(
It.IsAny<string>())).Returns<string>(s => s);
Mock<HttpContextBase> mockContext = new Mock<HttpContextBase>();
mockContext.Setup(m => m.Request).Returns(mockRequest.Object);
mockContext.Setup(m => m.Response).Returns(mockResponse.Object);
return mockContext.Object;
}
private void TestRouteMatch(string url?? string controller??
string action?? object routeProperties = null??
string httpMethod = "GET")
{
//????
RouteCollection routes = new RouteCollection();
MvcApplication.RegisterRoutes(routes);
//????
RouteData result = routes.GetRouteData(CreateHttpContext(url?? httpMethod));
//????
Assert.IsNotNull(result);
Assert.IsTrue(TestIncomingRouteResult(result?? controller?? action?? routeProperties));
}
private bool TestIncomingRouteResult(RouteData routeResult??
string controller?? string action?? object propertySet = null)
{
Func<object?? object?? bool> valCompare = (v1?? v2) =>
{
return StringComparer.InvariantCultureIgnoreCase.Compare(v1?? v2) == 0;
};
bool result = valCompare(routeResult.Values["controller"]?? controller)
&& valCompare(routeResult.Values["action"]?? action);
if (propertySet != null)
{
PropertyInfo[] propInfo = propertySet.GetType().GetProperties();
foreach (PropertyInfo pi in propInfo)
{
if (!(routeResult.Values.ContainsKey(pi.Name) &&
valCompare(routeResult.Values[pi.Name]?? pi.GetValue(propertySet?? null))))
{
result = false;
break;
}
}
}
return result;
}
private void TestRouteFail(string url)
{
//????
RouteCollection routes = new RouteCollection();
MvcApplication.RegisterRoutes(routes);
//????
RouteData result = routes.GetRouteData(CreateHttpContext(url));
//????
Assert.IsTrue(result == null || result.Route == null);
}
[TestMethod]
public void TestIncomingRoutes()
{
TestRouteMatch("~/Admin/Index"?? "Admin"?? "Index");
TestRouteMatch("~/One/Two"?? "One"?? "Two");
TestRouteFail("~/Admin/Index/Segment");
TestRouteFail("~/Admin");
}
}
}

????5???????????????????Test -> Run -> All Tests in Solution????е???????