Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
528 views
in Technique[技术] by (71.8m points)

asp.net mvc 3 - MVC3 unit testing response code

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...