测试代码
1 [TestMethod]
2 public void TestMvcApplicationRoute()
3 {
4 RouteCollection routes = new RouteCollection();
5 MvcApplication.RegisterRoutes(routes);
6 var httpContextMock = new Mock
7 httpContextMock.Setup(c => c.Request
8 .AppRelativeCurrentExecutionFilePath).Returns("~/Home/Index");
9
10 RouteData routeData = routes.GetRouteData(httpContextMock.Object);
11
12 Assert.IsNotNull(routeData, "Should have found the route");
13 Assert.AreEqual("Home", routeData.Values["Controller"]);
14 Assert.AreEqual("Index", routeData.Values["action"]);
15 Assert.AreEqual("", routeData.Values["id"]);
16 }
下面我们解释一下上面的代码
RouteCollection routes = new RouteCollection();
MvcApplication.RegisterRoutes(routes);
这两行的意思是将routes按照默认路由代码中的格式生成相应的路由。
var httpContextMock = new Mock
httpContextMock.Setup(c => c.Request.AppRelativeCurrentExecutionFilePath).Returns("~/Home/Index");
这两行的意思是创建一个HttpContextBase的mock类,这里我们使用的是MoQ框架来生成mock类(参见:MoQ(基于.net3.5,c#3.0的mock框架)简单介绍 )。
文章来源于领测软件测试网 https://www.ltesting.net/