How about doing it in a more MVCish way:
public ActionResult Http500()
{
return new HttpStatusCodeResult(500, "An error occurred whilst processing your request.");
}
and then:
// arrange
var sut = new HomeController();
// act
var actual = sut.Http500();
// assert
Assert.IsInstanceOfType(actual, typeof(HttpStatusCodeResult));
var httpResult = actual as HttpStatusCodeResult;
Assert.AreEqual(500, httpResult.StatusCode);
Assert.AreEqual("An error occurred whilst processing your request.", httpResult.StatusDescription);
or if you insist on using the Response object you could create a fake one:
// arrange
var sut = new HomeController();
var request = new HttpRequest("", "http://example.com/", "");
var response = new HttpResponse(TextWriter.Null);
var httpContext = new HttpContextWrapper(new HttpContext(request, response));
sut.ControllerContext = new ControllerContext(httpContext, new RouteData(), sut);
// act
var actual = sut.Http500();
// assert
Assert.AreEqual(500, response.StatusCode);
Assert.AreEqual("An error occurred whilst processing your request.", response.StatusDescription);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…