2011-01-06 67 views
0

我正在使用Steve Sandersons MVC框架书(第二版)中的一些代码。我有一个名为Category的类,它使用我在菜单中使用的其他属性构建RouteValueDictionary。在网站上,它工作正常,路由是正确的,链接输出如表所示。但是,我努力为他们创建一些测试。这是我的分类等级:在asp.net中测试路由MVC

public class Category 
{ 
    public Category() 
    { 
    } 
    public Category(int id, string title, int sortOrder, int? parentId, bool isMainCategory, bool isVisible) 
    { 
     Id = id; 
     Title = title ?? "Home"; 
     SortOrder = sortOrder; 
     ParentId = parentId; 
     IsMainCategory = isMainCategory; 
     IsVisible = isVisible; 
    } 

    private RouteValueDictionary routeValues; 

    public int Id { get; set; } 
    public string Title { get; set; } 
    public int? ParentId { get; set; } 
    public bool IsMainCategory { get; set; } 
    public bool IsVisible { get; set; } 
    public int SortOrder { get; set; } 

    public List<Category> SubCategories { get; set; } 

    public RouteValueDictionary RouteValues { 
     get 
     { 
      if (routeValues == null) 
      { 
       routeValues = new RouteValueDictionary(new 
       { 
        controller = "Products", 
        action = "Index", 
        cat = Id, 
        categoryName = UrlWorker.CleanUrl(Title) 
        //, 
        //page = 1, 
        //isParent = parent 
       }); 
      } 
      return routeValues; 
     } 
     set 
     { 
      routeValues = value; 

     } 
    } 
} 

这里是我已经设置了测试:

[TestMethod] 
    public void CategoryGoesToCorrectRoute() 
    { 
     List<Category> list = categoryService.GetVisibleCategories(); 
     Assert.AreEqual("/products/3/category-3", GenerateUrlViaMocks(new RouteValueDictionary(new 
     { 
      controller = "products", 
      action = "Index", 
      cat = 3, 
      categoryName = "category-3" 
     }))); 

     //list[0].RouteValues)); 
    } 

而且GenerateUrlViaMocks方法:

private string GenerateUrlViaMocks(object values) 
    { 
     // Arrange (get the routing config and test context) 
     RouteCollection routeConfig = new RouteCollection(); 
     RegisterAllAreas(routeConfig); 
     MvcApplication.RegisterRoutes(routeConfig); 
     var mockContext = MakeMockHttpContext(null); 

     RouteData routeData = routeConfig.GetRouteData(mockContext.Object); 

     RequestContext context = new RequestContext(mockContext.Object, routeData); 
     // Act (generate a URL) 
     return UrlHelper.GenerateUrl(null, null, null, 
     new RouteValueDictionary(values), routeConfig, context, true); 
    } 

最后,这里是我的路由表:

public static void RegisterRoutes(RouteCollection routes) 
    { 
     routes.MapRoute(
      "SizeGuide", 
      "products/sizing", 
      new { controller = "products", action = "Sizing" }, 
      new string[] { "EliteFightKit.Web.Controllers" } 
     ); 
     routes.MapRoute(
      "ViewCategory", 
      "products/{cat}/{categoryName}", 
      new { controller = "products", action = "Index", cat = 0, categoryName = "" }, 
      new string[] { "EliteFightKit.Web.Controllers" } 
     ); 

     routes.MapRoute(
      "ViewProduct", 
      "products/{cat}/{id}/{productName}", 
      new { controller = "products", action = "Details", cat = "", id = "", productName = "" }, 
      new string[] { "EliteFightKit.Web.Controllers" } 
     ); 

     routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 

     routes.MapRoute(
      "Sitemap", 
      "sitemap", 
      new { controller = "Sitemap", action = "Index" } 
     ); 
     routes.MapRoute(
      "Default", 
      "{controller}/{action}/{id}", 
      new { action = "Index", id = "" }, 
      new string[] { "EliteFightKit.Web.Controllers" } 
     ); 
     routes.MapRoute(
      "Root", 
      "", 
      new { controller = "Home", action = "Index", id = "" } 
     ); 
    } 

Th我的问题是测试的实际结果是回来作为/ products/sizing?Count = 4 ...

...是一堆url编码'代码',基本上使它成为一个字典(对不起,我不能打印实际的错误,此时我不在机器上,这使我能够运行测试)。鉴于这在网站上运行,我认为这是我在GenerateUrlViaMocks中设置的方式的一个问题。被传递到该方法的routeValues是正确的,所以我非常困惑,为什么它没有采用我在路由参数中有'cat'和'categoryName'参数的事实。我哪里错了?

劳埃德

回答

2

我发现这个问题是在这条线:

return UrlHelper.GenerateUrl(null, null, null, 
     new RouteValueDictionary(values), routeConfig, context, true); 

其中已经一个RouteValueDictionary的值,正在转化成另一种RouteValueDictionary。因此,所有的属性,例如Count,Keys,Values都被作为下一个RVD中的Key值使用。

我改成了这样:

return UrlHelper.GenerateUrl(null, null, null, 
      (RouteValueDictionary)values, routeConfig, context, true); 

和它的作品。 :)

0

嘛,你不指定路线名称,所以"SizeGuide",其中没有一个有用的约束,但确实有默认值,则匹配。这可能是错误的。您需要在呼叫GenerateUrl时指定路线名称或为路线添加约束。