I have a controller within MVC3 which needs to return a response code 500 if something goes wrong. I am doing this by returning a view object and setting http response code to equal 500 (I have checked this in firebug and all is working great).
public ActionResult http500()
{
ControllerContext.HttpContext.Response.StatusCode = 500;
ControllerContext.HttpContext.Response.StatusDescription = "An error occurred whilst processing your request.";
return View();
}
The problem I have now is I need to be able to write a unit test which checks the response code. I have tried accessing the response code in several different ways both through the ViewResult object and the Controller context.
Neither way gives me the response code I have set in the controller.
[TestMethod()]
public void http500Test()
{
var controller = new ErrorController();
controller.ControllerContext = new ControllerContext(FakeHttpObject(), new RouteData(), controller);
ViewResult actual = controller.http500() as ViewResult;
Assert.AreEqual(controller.ControllerContext.HttpContext.Response.StatusCode, 500);
}
How would I go about getting the response code 500 from the controller or is this more of an integration testing thing.
See Question&Answers more detail:os