问题
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