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

The Archive Base Latest Questions

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

i have an actionresult that overrides Execute (as you do) and the actionresult basically

  • 0

i have an actionresult that overrides Execute (as you do) and the actionresult basically receives a model, serializes it, and writes it out to the reponse via response.write(model as text).

ive done some looking around and i cant see an easy way to test the response is correct. i think the best way is to test instantiate the custom actionresult & model, call the execute method, and then somehome inspect the controllercontexts’ response. Only problem is, how do i get the reponses output as a string? i read somewhere that you cant get the responses outputstream as text? if so, how do i verify that my content is being written to the response stream? i can test my serialiser to reduce the risk, but id like to get some coverage over the ExecuteResult override…

public class CustomResult: ActionResult
    {

        private readonly object _contentModel;
        private readonly ContentType _defaultContentType;

        public CustomResult(object contentModel, ContentType defaultContentType)
        {
            _contentModel = contentModel;
            _defaultContentType = defaultContentType;
        }

        public override void ExecuteResult(ControllerContext context)
        {
            context.HttpContext.Response.Write(serialized model);
        }

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

    You can get the output as text, but you do have to do a bit of extra work to make it all testable.

    You’ll need MVCContrib.TestHelper to get mocks set up for all the MVC components. On top of that I have a bit of code setting up access to the relevant parts of the request:

    public class CustomTestControllerBuilder : TestControllerBuilder
    {
    
        public CustomTestControllerBuilder()
        {
            var httpContext = new Moq.Mock<HttpContextBase>();
            var request = new Moq.Mock<HttpRequestBase>();
            var response = new Moq.Mock<HttpResponseBase>();
            var server = new Moq.Mock<HttpServerUtilityBase>();
            var _cache = HttpRuntime.Cache;
    
            httpContext.Setup(x=> x.Request).Returns(request.Object);
            httpContext.Setup(x => x.Response).Returns(response.Object);
            httpContext.Setup(x => x.Session).Returns(Session);
            httpContext.Setup(x => x.Server).Returns(server.Object);
            httpContext.Setup(x => x.Cache).Returns(_cache);
    
    
            var items = new Dictionary<object, object>();
            httpContext.Setup(x => x.Items).Returns(items);
    
    
            QueryString = new NameValueCollection();
            request.Setup(x => x.QueryString).Returns(QueryString);
    
            Form = new NameValueCollection();
            request.Setup(x => x.Form).Returns(Form);
    
            request.Setup(x => x.AcceptTypes).Returns((Func<string[]>)(() => AcceptTypes));
    
            var files = new WriteableHttpFileCollection();
            Files = files;
            request.Setup(x => x.Files).Returns(files);
    
            Func<NameValueCollection> paramsFunc = () => new NameValueCollection { QueryString, Form };
            request.Setup(x => x.Params).Returns(paramsFunc);
    
            request.Setup(x => x.AppRelativeCurrentExecutionFilePath).Returns(
                    (Func<string>)(() => AppRelativeCurrentExecutionFilePath));
            request.Setup(x => x.ApplicationPath).Returns((Func<string>)(() => ApplicationPath));
            request.Setup(x => x.PathInfo).Returns((Func<string>)(() => PathInfo));
            request.Setup(x => x.RawUrl).Returns((Func<string>)(() => RawUrl));
            response.SetupProperty(x => x.Status);
            httpContext.SetupProperty(x=>x.User);
    
            var ms = new MemoryStream(65536);
            var sw = new StreamWriter(ms);
            response.SetupGet(x=>x.Output).Returns(sw);
            response.SetupGet(x => x.OutputStream).Returns(ms);
    
            response.Setup(x => x.Write(It.IsAny<string>())).Callback((string s) => { sw.Write(s); });
            response.Setup(x => x.Write(It.IsAny<char>())).Callback((char s) => { sw.Write(s); });
            response.Setup(x => x.Write(It.IsAny<object>())).Callback((object s) => { sw.Write(s); });
    
            //_mocks.Replay(HttpContext);
            //_mocks.Replay(request);
            //_mocks.Replay(response);
    
            TempDataDictionary = new TempDataDictionary();
    
    
            HttpContext = httpContext.Object;
        }
    
    }
    }
    

    Note that OutputStream is now a MemoryStream rather than an actual web connection. With that set up you just need a few more lines to get the output that would be sent to the client:

     Result.ExecuteResult(c.ControllerContext);
     var ms = (MemoryStream)c.HttpContext.Response.OutputStream;
     c.HttpContext.Response.Output.Flush();
     ResultHtml = new UTF8Encoding().GetString(ms.GetBuffer());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a pluggable framework that returns ActionResult objects that render things to a
I have multiple controller actions that takes an id public ActionResult Get(int? id) {
I have an ASP.NET MVC form that when submitted can return either an ActionResult
I am using ASP.NET MVC 1.0. I have an ActionResult which receives a form
I have a web application that fetches a lot of content via ajax. For
I have an action that looks like this: [Post] [PopulateModelFromId] public ActionResult ChangeName( string
I have a model that I would like to serialize to an xml with
I have made a base controller class that overrides OnActionExecuting as follows: public class
I have created a custom Model Binder that inherits from DefaultModelBinder, and overriding the
I have a database that stores images which can be access via a primary

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.