【发布时间】:2011-01-06 21:22:59
【问题描述】:
我正在使用 Steve Sandersons MVC 框架书(第 2 版)中的一些代码。我有一个名为 Category 的类,它使用它的其他属性构建一个 RouteValueDictionary,然后我在菜单中使用这些属性。在网站上它工作正常,路由正确,链接输出如预期。但是,我正在尝试为他们创建一些测试。这是我的类别类:
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 = "" }
);
}
我遇到的问题是测试实际结果返回为 /products/sizing?Count=4...
... 是一堆 url 编码的“代码”,基本上可以看出它是一个字典(抱歉,当我离开使我能够运行测试的机器时,我无法打印实际错误)。鉴于这在网站上有效,我认为这是我在 GenerateUrlViaMocks 中设置的方式的问题。传递给该方法的 routeValues 是正确的,所以我真的很困惑为什么它没有发现我的路由参数中有一个 'cat' 和 'categoryName' 参数这一事实。我哪里错了?
劳埃德
【问题讨论】:
标签: asp.net-mvc unit-testing asp.net-mvc-2 routes url-routing