Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7695029
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T21:27:12+00:00 2026-05-31T21:27:12+00:00

I have a controller within MVC3 which needs to return a response code 500

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T21:27:13+00:00Added an answer on May 31, 2026 at 9:27 pm

    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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using MVC3 I have this in my controller. How do I enumerate this within
As explained in this question , I have a view controller that needs to
I have a controller with 2 actions that each return the response from the
I have a view controller and I am intercepting the touches on links within
I have a number of existing methods within a controller. Each one of those
I have a Tab Bar Controller created in Interface Builder Within the Tab Bar
I using the Rails resource_controller plugin. I have a ReleasesController, which is nested within
I am using MVC3, and have some logic for changing the culture which all
In MVC/MVP style applications that have Controller/Presenter classes within the Client Application assembly and
Using Entity Framework 4, MVC3, C# I have created a complex type within the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.