0

我以前没有使用 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'?

4

1 回答 1

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");
于 2012-09-14T13:36:05.213 回答