我正在使用 mvcintegrationtesting 框架: https ://github.com/chrisortman/MvcIntegrationTest
这是基于:http: //blog.stevensanderson.com/2009/06/11/integration-testing-your-aspnet-mvc-application/
发布了以下模型,但事先将其解析为字典。
[Serializable]
public class PatientViewModel
{
public PatientViewModel()
{
Appointments = new EditableList<AppointmentListViewModel>();
}
public Guid Id { get; set; }
[Required(ErrorMessageResourceType = typeof (Resources), ErrorMessageResourceName = "RequiredErrorMessage")]
[Display(Name = "FirstName", ResourceType = typeof (Resources))]
public string FirstName { get; set; }
[Required(ErrorMessageResourceType = typeof (Resources), ErrorMessageResourceName = "RequiredErrorMessage")]
[Display(Name = "LastName", ResourceType = typeof (Resources))]
public string LastName { get; set; }
[Display(Name = "PatientName", ResourceType = typeof (Resources))]
public string FullName
{
get { return string.Format("{0} {1}", FirstName, LastName); }
}
[Required(ErrorMessageResourceType = typeof (Resources), ErrorMessageResourceName = "RequiredErrorMessage")]
[Display(Name = "BirthDate", ResourceType = typeof (Resources))]
public DateTime BirthDate { get; set; }
[Display(Name = "Appointment", ResourceType = typeof (Resources))]
public DateTime Appointment { get; set; }
[Required(ErrorMessageResourceType = typeof (Resources), ErrorMessageResourceName = "RequiredErrorMessage")]
[Display(Name = "PhoneNumber", ResourceType = typeof (Resources))]
public string PhoneNumber { get; set; }
[Required(ErrorMessageResourceType = typeof (Resources), ErrorMessageResourceName = "RequiredErrorMessage")]
[Display(Name = "PhoneNumber2", ResourceType = typeof (Resources))]
public string PhoneNumber2 { get; set; }
[Required(ErrorMessageResourceType = typeof (Resources), ErrorMessageResourceName = "RequiredErrorMessage")]
[Display(Name = "Email", ResourceType = typeof (Resources))]
public string Email { get; set; }
[Display(Name = "ExtraInfo", ResourceType = typeof (Resources))]
public string ExtraInfo { get; set; }
[Display(Name = "Duration", ResourceType = typeof (Resources))]
public int Duration { get; set; }
public AddressViewModel AddressViewModel { get; set; }
[UniqueItemsInList]
public List<Doctor> Doctors { get; set; }
[XmlIgnore]
public List<AppointmentListViewModel> Appointments { get; set; }
}
这是post方法
SimulateBrowsingSession(session =>
{
var queryString = string.Format("/Patient/Edit/{0}", patient.Id.ToString());
var result = session.Get(queryString);
var model = result.GetViewModel<PatientViewModel>();
var postresult = session.Post(queryString, model);
});
上面的结果没有调用控制器。因此我的第一个问题是:为什么我在控制器的编辑帖子中的断点没有被命中。如果我通过我的模型的空值,它确实会命中。
接下来我尝试了以下方法:
var postresult = session.Post(queryString, new NameValueCollection
{
{"Id", patient.Id.ToString()},
{"FirstName", patient.FirstName},
{"LastName", patient.LastName}
});
这会导致控制器编辑发布方法命中,但仅填写了 Id,名字和姓氏字段为空。
第二个问题:为什么会这样?第三个问题:我如何解决 Q1 和 Q2?
编辑:控制器代码
[HttpPost]
public ActionResult Edit(PatientViewModel viewModel)
{
try
{
if (ModelState.IsValid)
{
try
{
var item = Map.This(viewModel).To<Patient>();
_patientService.Update(item);
return RedirectToAction("Details", new { id = item.Id });
}
catch
{
AddSelectLists();
return View(viewModel);
}
}
AddSelectLists();
return View(viewModel);
}
catch
{
return View();
}
}
谢谢,
伊恩