mvccontrib test helper and verifying http post routes and parameters

独自空忆成欢 提交于 2019-12-13 04:39:45

问题


Here's an example. Assuming you have the following action:

public AccountController : Controller
{
    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Foo(string id) 
    {
        return View();
    }
}

And the following route registered:

RouteTable.Routes.MapRoute(
    "Default",
    "{controller}/{action}/{id}",
    new { controller = "home", action = "index", id = "" }
);

I test it like this:

var routeData = "~/account/foo".WithMethod(HttpVerbs.Post);
routeData.Values["id"] = "123";
routeData.ShouldMapTo<AccountController>(c => c.Foo("123"));

But this work too with Get, and don't have to work:

var routeData = "~/account/foo".WithMethod(HttpVerbs.Get);
routeData.Values["id"] = "123";
routeData.ShouldMapTo<AccountController>(c => c.Foo("123"));

Why?

UPDATE 1:

My Real methods are:

public ActionResult Novo()
{
    NovoViewModel novoViewModel = new NovoViewModel();
    novoViewModel.Sexos = GetSexos();
    novoViewModel.Status = GetStatus();

    return View(novoViewModel);
}

[HttpPost]
public ActionResult Novo(NovoViewModel novoViewModel)
{
    //Validações de autenticação
    if (_authenticationService.IsUserRegistered(novoViewModel.NomeUsuario))
    {
        ModelState.AddModelError("ExistingUsername", String.Format(ViewModelValidations.ExistingUsername, "Usuário"));
    }

    if (_usuarioRepository.PegaUsuarioPorEmail(novoViewModel.Email) != null)
    {
        ModelState.AddModelError("ExistingEmail", String.Format(ViewModelValidations.ExistingEmail, "Email"));
    }

    if (ModelState.IsValid) {
        Usuario usuario = new Usuario()
                                {
                                    Nome = novoViewModel.Nome,
                                    Email = novoViewModel.Email,
                                    Telefone = novoViewModel.Telefone,
                                    DataNascimento = Convert.ToDateTime(novoViewModel.DataNascimento),
                                    Sexo = !String.IsNullOrEmpty(novoViewModel.SexoSelecionado) ? (Sexo?)(Convert.ToByte(novoViewModel.SexoSelecionado)) : null,
                                    Credencial = new Credencial()
                                                    {
                                                        NomeUsuario = novoViewModel.NomeUsuario,
                                                        Senha = novoViewModel.Senha,
                                                        Status = (Status)Convert.ToByte(novoViewModel.SexoSelecionado)
                                                    }
                                };

        _usuarioRepository.Add(usuario);

        _dbContext.SaveChanges();

        return RedirectToAction("Index");
    }

    novoViewModel.Sexos = GetSexos();
    novoViewModel.Status = GetStatus();

    return View(novoViewModel);
}

回答1:


Try putting id in url that you're testing:

var routeData = "~/account/foo/123".WithMethod(HttpVerbs.Post);


来源:https://stackoverflow.com/questions/7164967/mvccontrib-test-helper-and-verifying-http-post-routes-and-parameters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!