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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T13:03:02+00:00 2026-06-11T13:03:02+00:00

I followed code sample in Steve Sanderson’s MVC3 book , there is a piece

  • 0

I followed code sample in Steve Sanderson’s MVC3 book, there is a piece of ControlContext mock code there I can setup form and query string values.

In order to support TryUpdateModel in unit test I made some change to the code, for example I have changed data type of formValues to FormCollection formValues and I added following codes to make it work:

        // form Values values setup
        this.Request.Setup(x => x.Form).Returns(formValues);

        // wire up form with value provider
        if (formValues != null)
        {
            onController.ValueProvider = formValues.ToValueProvider();
        }

Meanwhile I would like to do the same thing to querystring collection, but could not find equivalent class to support such a function.

Who has any idea what shall I do to query string?

  • 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-06-11T13:03:03+00:00Added an answer on June 11, 2026 at 1:03 pm

    Eventually, this is my solution

    using System;
    using System.Collections.Generic;
    using System.Web;
    using System.Web.Mvc;
    using System.Web.Routing;
    using Moq;
    
    /// <summary>
    /// A helper class for MVC projects' unit testing.
    /// </summary>
    public class ContextMocks
    {
        /// <summary>
        /// Initializes a new instance of the ContextMocks class.
        /// </summary>
        /// <param name="onController">The controller to mock.</param>
        /// <param name="identityName">The fake security identity name for controller.</param>
        /// <param name="queryStrings">Fake query string values of the Http context.</param>
        /// <param name="formValues">Fake form values of the Http context.</param>
        /// <param name="isNewSession">Enables us to mock if the session was created for the current thread or not.</param>
        public ContextMocks(Controller onController, string identityName, FormCollection queryStrings, FormCollection formValues, bool isNewSession = true)
        {
            this.Cookies = new HttpCookieCollection();
    
            this.Request = new Mock<HttpRequestBase>();
    
            this.Response = new Mock<HttpResponseBase>();
    
            this.Server = new Mock<HttpServerUtilityBase>();
    
            // Define all the common context objects, plus relationships between them
            this.HttpContext = new Mock<HttpContextBase>();
            this.HttpContext.Setup(x => x.Request).Returns(this.Request.Object);
            this.HttpContext.Setup(x => x.Response).Returns(this.Response.Object);
            this.HttpContext.Setup(x => x.Session).Returns(new FakeSessionState(isNewSession));
            this.HttpContext.Setup(x => x.Application).Returns(new FakeApplicationState());
            this.HttpContext.Setup(x => x.User.Identity.Name).Returns(identityName);
            this.HttpContext.Setup(x => x.Server).Returns(this.Server.Object);
    
            // cookie setup
            this.Request.Setup(x => x.Cookies).Returns(this.Cookies);
            this.Response.Setup(x => x.Cookies).Returns(this.Cookies);
    
            // query string setup
            this.Request.Setup(x => x.QueryString).Returns(queryStrings);
    
            // wire up form with value provider
            if (queryStrings != null)
            {
                onController.ValueProvider = queryStrings.ToValueProvider();
            }
    
            // form Values values setup
            this.Request.Setup(x => x.Form).Returns(formValues);
    
            // wire up form with value provider
            if (formValues != null)
            {
                onController.ValueProvider = formValues.ToValueProvider();
            }
    
            // Apply the mock context to the supplied controller instance
            RequestContext rc = new RequestContext(this.HttpContext.Object, new RouteData());
            onController.ControllerContext = new ControllerContext(rc, onController);
        }
    
        /// <summary>
        /// Initializes a new instance of the ContextMocks class. By using this constructor the mock will also enable UrlHelper mocking
        /// by using routing table setting.
        /// </summary>
        /// <param name="onController">The controller to mock.</param>
        /// <param name="registerRouteTable">
        /// A delegate to the function to register route table. 
        /// Typically it is MvcApplication.RegisterRoutes function in global.asax.cs.
        /// </param>
        /// <param name="identityName">The fake security identity name for controller.</param>
        /// <param name="queryStrings">Fake query string values of the Http context.</param>
        /// <param name="formValues">Fake form values of the Http context.</param>
        /// <param name="isNewSession">Enables us to mock if the session was created for the current thread or not.</param>
        public ContextMocks(
            Controller onController, 
            Action<RouteCollection> registerRouteTable, 
            string identityName, 
            FormCollection queryStrings, 
            FormCollection formValues,
            bool isNewSession = true)
            : this(onController, identityName, queryStrings, formValues, isNewSession)
        {
            this.Response.Setup(x => x.ApplyAppPathModifier(It.IsAny<string>())).Returns<string>(s => s);
    
            // Arrange (get the routing config and test context)
            RouteCollection routeConfig = new RouteCollection();
            registerRouteTable(routeConfig);
            onController.Url = new UrlHelper(new RequestContext(this.HttpContext.Object, new RouteData()), routeConfig);
        }
    
        /// <summary>
        /// Gets Http Context mock.
        /// </summary>
        public Mock<HttpContextBase> HttpContext { get; private set; }
    
        /// <summary>
        /// Gets Http request mock.
        /// </summary>
        public Mock<HttpRequestBase> Request { get; private set; }
    
        /// <summary>
        /// Gets Http response mock.
        /// </summary>
        public Mock<HttpResponseBase> Response { get; private set; }
    
        /// <summary>
        /// Gets the mock server object.
        /// </summary>
        public Mock<HttpServerUtilityBase> Server { get; private set; }
    
        /// <summary>
        /// Gets Route data.
        /// </summary>
        public RouteData RouteData { get; private set; }
    
        /// <summary>
        /// Gets or sets A collection used to hold fake cookie values.
        /// </summary>
        private HttpCookieCollection Cookies
        {
            get;
            set;
        }
    
        /// <summary>
        /// Use queryStringCollectionProvider fake HttpSessionStateBase, because it's hard to mock it with Moq.
        /// </summary>
        private class FakeSessionState : HttpSessionStateBase
        {
            /// <summary>
            /// Enables us to mock whether the session was created for the current thread or not.
            /// </summary>
            private bool isNewSession;
    
            /// <summary>
            /// Fake session state collection.
            /// </summary>
            private Dictionary<string, object> items = new Dictionary<string, object>();
    
            /// <summary>
            /// Initializes a new instance of the FakeSessionState class.
            /// </summary>
            /// <param name="isNewSession">Enables us to mock if the session was created for the current thread or not.</param>
            public FakeSessionState(bool isNewSession)
            {
                this.isNewSession = isNewSession;
            }           
    
            /// <summary>
            /// Gets a value that indicates whether the session was created during the current request.
            /// </summary>
            public override bool IsNewSession
            {
                get
                {
                    return this.isNewSession;
                }
            }
    
            /// <summary>
            /// An indexer to access the fake session item.
            /// </summary>
            /// <param name="name">Name of fake session key.</param>
            /// <returns>Fake session value.</returns>
            public override object this[string name]
            {
                get { return this.items.ContainsKey(name) ? this.items[name] : null; }
                set { this.items[name] = value; }
            }
    
            /// <summary>
            /// Empties the contents of the session.
            /// </summary>
            public override void Abandon()
            {
                this.items = new Dictionary<string, object>();
            }
        }
    
        /// <summary>
        /// Use queryStringCollectionProvider fake HttpApplicationStateBase, because it's hard to mock it with Moq.
        /// </summary>
        private class FakeApplicationState : HttpApplicationStateBase
        {
            /// <summary>
            /// Fake application state collection.
            /// </summary>
            private Dictionary<string, object> items = new Dictionary<string, object>();
    
            /// <summary>
            /// An indexer to access the fake application item.
            /// </summary>
            /// <param name="name">Name of fake application key.</param>
            /// <returns>Fake application value.</returns>
            public override object this[string name]
            {
                get { return this.items.ContainsKey(name) ? this.items[name] : null; }
                set { this.items[name] = value; }
            }
    
            /// <summary>
            /// Mock out of the lock method.
            /// </summary>
            public override void Lock()
            {
            }
    
            /// <summary>
            /// Mock out of the unlock method.
            /// </summary>
            public override void UnLock()
            {
            }
    
            /// <summary>
            /// Adds the fake object to the application object.
            /// </summary>
            /// <param name="name">The key</param>
            /// <param name="value">The value being added in conjunction with the key.</param>
            public override void Add(string name, object value)
            {
                this.items.Add(name, value);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using mvc3 to develop an database application and followed the Code-First Tutorial
I have followed the instructions found at http://code.google.com/p/pubsubhubbub/wiki/DeveloperGettingStartedGuide to setup a hub. When I
I have followed the sample code on the Twilio github page and it doesn't
so in some sample code from this upcoming Core Audio Book i've encountered an
Im trying to call python functions from C code, and i followed a sample
The following sample code is from Joe Armstrong's Programming Erlang book: max(N) -> Max
I have followed the sample code below to upload a zip file in the
I followed Apple's GLPaint paint application and tried to modify it.. In sample code
I was going through code samples of Begining C# 3.0 and followed the sample
I have followed the sample code on Apple's Quartz programming guide (Listing 2-5) and

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.