Question

I've not used the MvcContrib for unit testing before and I'm having a bit of trouble running some of the tests.

I have the following test method:

[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));
}

Which matches to the following method in my Controller

    [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);
    }

I get the following error and I'm at a loss to figure out why.

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'?

Était-ce utile?

La solution

The token names are case sensitive and should match the names of your action parameters and you need to use the Values collection instead of the DataTokens:

So because your action looks like this:

Create(bool? popup, int? webDirectoryId, int? localeId, int? layoutId)

You need to use the same lower-case token names and Values collection:

getRouteData.Values.Add("popup", "true");
getRouteData.Values.Add("webDirectoryId", "99");
getRouteData.Values.Add("localeId", "88");
getRouteData.Values.Add("layoutId", "77");
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top