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,执行单元测试。