MVC3 unit testing response code

2019-01-17 23:27发布

问题:

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.

回答1:

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);


回答2:

What is FakeHttpObject()? Is it a mock created using Moq? In that case you need to setup setters and getters to store the actual values somewhere. Mock<T>doesn't provide any implementation for properties and methods. When setting a value of property literally nothing happens and the value is 'lost'.

Another option is to provide a fake context that is a concrete class with real properties.