我正在学习ASP.NET MVC,并尝试为单元测试创建模拟表单请求。
我正在使用RhinoMocks。
我已查看以下网站,但无法使其正常工作。
将此翻译为中文:http://blog.maartenballiauw.be/post/2008/03/19/ASPNET-MVC-Testing-issues-Q-and-A.aspx
Update: Controller Code:
/// <summary>
/// Creates a new entry
/// </summary>
/// <returns></returns>
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create([Bind()]Person person)
{
if (Request.Form["DateOfBirth"].ToString() == "")
{
TempData["message"] = "Please select a date of Birth";
ViewData["DateOfBirth"] = Request.Form["DateOfBirth"].ToString();
MvcValidationAdapter.TransferValidationMessagesTo(ViewData.ModelState, person.ValidationMessages);
return View();
}
else
{
if (person.IsValid())
{
person.DateOfBirth = Convert.ToDateTime(Request.Form["DateOfBirth"]);
personRepository.SaveOrUpdate(person);
TempData["message"] = person.Firstname + " was successfully added";
return RedirectToAction("Create", "OrderDetails", new { id = person.ID });
}
else
{
ViewData["DateOfBirth"] = Request.Form["DateOfBirth"].ToString();
MvcValidationAdapter.TransferValidationMessagesTo(ViewData.ModelState, person.ValidationMessages);
return View();
}
}
}