之前我没有使用MvcContrib进行单元测试,而且在运行某些测试时遇到了一些麻烦。
我有以下测试方法:
[TestMethod]
public void Create_GET_Route_Maps_To_Action()
{
RouteData getRouteData = "~/Interface/Pages/Create".WithMethod(HttpVerbs.Get);
getRouteData.DataTokens.Add("Popup", "true");
getRouteData.DataTokens.Add("WebDirectoryId", "99");
getRouteData.DataTokens.Add("LocaleId", "88");
getRouteData.DataTokens.Add("LayoutId", "77");
getRouteData.ShouldMapTo<PagesController>(c => c.Create(true, 99, 88, 77));
}
哪个与我的控制器中的以下方法匹配
[HttpGet]
[Popup]
public ViewResult Create(bool? popup, int? webDirectoryId, int? localeId, int? layoutId)
{
PageCreateViewModel pageCreateViewModel = new PageCreateViewModel
{
WebDirectories = GetChildDirectories(pageService.GetAllDirectories().Where(d => d.IsActive).Where(d => d.ParentId == null), ""),
Layouts = Mapper.Map<List<SelectListItem>>(pageService.GetAllLayouts().OrderBy(l => l.Filename)),
Locales = localizationService.GetAllLocales().Where(l => l.IsActive).OrderBy(l => l.LocaleName).Select(l => new SelectListItem { Text = string.Format("{0} ({1})", l.LocaleName, l.IETFLanguageTag), Value = l.LocaleId.ToString() })
};
return View(pageCreateViewModel);
}
我收到以下错误,我不知道为什么会这样做。
MvcContrib.TestHelper.AssertionException: Value for parameter 'popup' did not match: expected 'True' but was ''; no value found in the route context action parameter named 'popup' - does your matching route contain a token called 'popup'?
答案 0 :(得分:1)
令牌名称区分大小写,并且应与您的操作参数的名称相匹配,您需要使用Values
集合而不是DataTokens
:
因为你的行动看起来像这样:
Create(bool? popup, int? webDirectoryId, int? localeId, int? layoutId)
您需要使用相同的小写令牌名称和Values
集合:
getRouteData.Values.Add("popup", "true");
getRouteData.Values.Add("webDirectoryId", "99");
getRouteData.Values.Add("localeId", "88");
getRouteData.Values.Add("layoutId", "77");