testing the controller in asp.net mvc
- by csetzkorn
Hi,
I would like to test the validation of submitted DTO. This is the bare bone of a controller create action:
[AcceptVerbs(HttpVerbs.Post)]
public RedirectToRouteResult Create(SomeDTO SomeDTO)
{
SomeObject SomeObject = null;
try
{
SomeObject = this.RepositoryService.getSomeObjectRepository().Create(SomeDTO, this.RepositoryService);
}
catch (BrokenRulesException ex)
{
ex.AddModelStateErrors(ModelState, "Model");
}
catch (Exception e)
{
ModelState.AddModelError("Exception", e.Message);
}
TempData["ViewData"] = ViewData;
TempData["SomeDTO "] = SomeDTO;
return ModelState.IsValid ? RedirectToAction("SomeObjectDetail", new { Id = SomeObject.Id }) : RedirectToAction("Form");
}
The mechanics , although not relevant, is as follows: I have a strongly typed view = form which submits a dto to this action which either returns the form or the details page of the created object.
I would like to unit test whether the Model contains certain key/errorMessage combinations given some invalid dto. Did someone do similar stuff? Any pointers would be very much appreciated.
Thanks.
Best wishes,
Christian