2011-01-06 78 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時指定路線名稱或爲路線添加約束。