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 3399532
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T04:46:57+00:00 2026-05-18T04:46:57+00:00

The Question… What is the best way to Unit Test the string response and

  • 0

The Question…

What is the best way to Unit Test the string response and content type from several Controller methods?

Using…

Each method returns an ActionResult, some of which are ViewResult responses. I’m using ASP.NET MVC 2 RTM and Moq.

Specifically…

I wish to obtain the TextWriter from HttpContext.Response and have it contain the full string response from an ActionResult.

Why?

1. Within Unit Tests

I want to test some specific if content does and does not exist with the output.

2. Runtime via worker thread

I use a background worker thread to update static content on remote servers, this content is the output from the Controllers and must be generated as such. Making requests to the same server via HTTP is not advisable because there are many 1000’s of files which are updated.

I see the same code being used at both Runtime and via Unit Tests, as it would be very similar?

Stumbling block 1

How to correctly setup mocking to not require Routes Or call RegisterRoutes and RegisterAllAreas have the call succeed, currently throws an exception deep inside BuildManagerWrapper::IBuildManager.GetReferencedAssemblies.

Sample Code

My mocking helpers look like this:

public static HttpContextBase FakeHttpContext()
{
    var context = new Mock<HttpContextBase>();
    var request = new Mock<HttpRequestBase>();
    var response = new Mock<HttpResponseBase>();
    var session = new Mock<HttpSessionStateBase>();
    var server = new Mock<HttpServerUtilityBase>();
    var writer = new StringWriter();

    var form = new NameValueCollection();
    var queryString = new NameValueCollection();
    request.Setup(r => r.Form).Returns(form);
    request.Setup(r => r.QueryString).Returns(queryString);

    context.Setup(ctx => ctx.Request).Returns(request.Object);
    context.Setup(ctx => ctx.Response).Returns(response.Object);
    context.Setup(ctx => ctx.Session).Returns(session.Object);
    context.Setup(ctx => ctx.Server).Returns(server.Object);
    context.Setup(ctx => ctx.Response.Output).Returns(writer);

    return context.Object;
}

public static void SetFakeControllerContext(this Controller controller)
{
    var httpContext = FakeHttpContext();
    var routeData = new RouteData();
    var routeData = RouteTable.Routes.GetRouteData(httpContext);
    ControllerContext context = new ControllerContext(new RequestContext(httpContext, routeData), controller);
    controller.ControllerContext = context;
}

And my current attempt at a TestMethod is as follows:

[TestMethod]
public void CodedJavaScriptAction_Should_Return_JavaScript_Response()
{
    // Arrange
    var controller = new CodedController();
    controller.SetFakeControllerContext();

    // Act
    var result = controller.CodedJavaScript(); // Response is made up as a ViewResult containing JavaScript.
    var controllerContext = controller.ControllerContext;
    var routeData = controllerContext.RouteData;
    routeData.DataTokens.Add("area", "Coded");
    routeData.Values.Add("area", "Coded");
    routeData.Values.Add("controller", "Coded");
    routeData.Values.Add("action", "CodedJavaScript");

    var response = controllerContext.HttpContext.Response;
    response.Buffer = true;
    var vr = result as ViewResult;
    vr.MasterName = "CodedJavaScript";

    result.ExecuteResult(controllerContext);

    // Assert
    var s = response.Output.ToString();
    Assert.AreEqual("text/javascript", response.ContentType);
    Assert.IsTrue(s.Length > 0);
    // @todo: Further tests to be added here.   

}

My area, views and shared files are:

–Areas\Coded\Controllers\CodeController.cs
–Areas\Coded\Views\Coded\CodedJavaScript.aspx
–Areas\Coded\CodedAreaRegistration.cs
–Views\Shared\CodedJavaScript.Master

EDIT: edited to now include both Unit Testing and Runtime execution. Thanks to @Darin Dimitrov for mentioning Integration Testing but there is now also a runtime element to this question.

EDIT: After some testing and review using some of the source code from MvcIntegrationTestFramework as referenced by alexn. Which uses AppDomain.CreateDomain and SimpleWorkerRequest to create a new request, I have found that it is not possible to create a new request via this method in a process that already has an active request, due to static values used. So this rules this method out.

Probably the same issue but I am now wondering if the result from a Partial View can be returned as a string more directly?

  • 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-18T04:46:58+00:00Added an answer on May 18, 2026 at 4:46 am

    What you are trying to achieve is no longer unit tests but integration testing because you are no longer testing your application components in isolation and there are great tools that allow you to do this like Selenium or Web Tests in the Ultimate versions of Visual Studio.

    The advantage of these tests is that they simulate user requests and cover the entire application behavior. So for a given user request you can assert that your application responds properly. The idea is that you write user scenarios, record them and then you could automate the execution of those tests to assert that your application responds as specified.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Question: I want to test an if statement in PostgreSQL: IF (SELECT COUNT(*) FROM
Question Using XSLT 1.0, given a string with arbitrary characters how can I get
Question : Can anyone tell me why my unit test is failing with this
Question from Object-Oriented JavaScript book: Imagine Array() doesn't exist and the array literal notation
Question I'd like the CSS background texture for my content area to begin immediately
Question Implement a function bool chainable(vector<string> v) , which takes a set of strings
Question: Is it possible to cluster an application which is using Socket.io for WebSocket
Question Does anyone know of a way to round a float to the nearest
Question : Is there a simple way to print out an array in Ruby
Question: Considering the hello-gl2 example from Android NDK r6b, is this example correct when

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.