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

  • Home
  • SEARCH
  • 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 773829
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T19:01:44+00:00 2026-05-14T19:01:44+00:00

i’m just learning to put in unit testing for my asp.net mvc when i

  • 0

i’m just learning to put in unit testing for my asp.net mvc when i came to learn about the mock and the different frameworks there is out there now.

after checking SO, i found that MOQ seems to be the easiest to pick up.
as of now i’m stuck trying to mock the Request.ServerVariables, as after reading this post, i’ve learned that it’s better to abstract them into property.

as such:

/// <summary>
        /// Return the server port
        /// </summary>
        protected string ServerPort
        {
            get
            {
                return Request.ServerVariables.Get("SERVER_PORT");
            }
        }

But i’m having a hard time learning how to properly mock this.
I have a home controller ActionResult function which grabs the user server information and proceed to create a form to grab the user’s information.

i tried to use hanselman’s mvcmockhelpers class but i’m not sure how to use it.

this is what i have so far…

[Test]
        public void Create_Redirects_To_ProductAdded_On_Success() 
        {

            FakeViewEngine engine = new FakeViewEngine();

            HomeController controller = new HomeController();
            controller.ViewEngine = engine;

            MvcMockHelpers.SetFakeControllerContext(controller);

            controller.Create();

            var results = controller.Create();

            var typedResults = results as RedirectToRouteResult;

            Assert.AreEqual("", typedResults.RouteValues["action"], "Wrong action");
            Assert.AreEqual("", typedResults.RouteValues["controller"], "Wrong controller");
        }

Questions:

  1. As of now i’m still getting null
    exception error when i’m running the
    test. So what am i missing here?
  2. And if i use the mvcmockhelpers
    class, how can i still call the
    request.verifyall function to ensure
    all the mocking are properly setup?
  • 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-14T19:01:45+00:00Added an answer on May 14, 2026 at 7:01 pm

    so basically i’ve put in all the references and using this function i was able to get it working. i didn’t use mvcmockhelpers class yet as i’m still trying to learn all of this.

    and for anyone who’s interested to see how i solved this, this is the code that i used.

    [Test]
    public void Create_Returns_ViewResult_On_Success() 
    {
      var server = new Mock<HttpServerUtilityBase>(MockBehavior.Loose);
      var response = new Mock<HttpResponseBase>(MockBehavior.Strict);
    
      var request = new Mock<HttpRequestBase>(MockBehavior.Strict);
      request.Setup(x => x.ApplicationPath).Returns("/");
      request.Setup(x => x.Url).Returns(new Uri("http://localhost"));
      request.Setup(x => x.ServerVariables).Returns(new System.Collections.Specialized.NameValueCollection{
        { "SERVER_NAME", "localhost" },
        { "SCRIPT_NAME", "localhost" },
        { "SERVER_PORT", "80" },
        { "HTTPS", "www.melaos.com" },
        { "REMOTE_ADDR", "127.0.0.1" },
        { "REMOTE_HOST", "127.0.0.1" }
      });
    
      var context = new Mock<HttpContextBase>();
      context.SetupGet(x => x.Request).Returns(request.Object);
      context.SetupGet(x => x.Response).Returns(response.Object);
      context.SetupGet(x => x.Server).Returns(server.Object);
    
      var controller = new HomeController();
      //MvcMockHelpers.SetFakeControllerContext(controller);
      controller.ControllerContext = new ControllerContext(context. Object, new RouteData(), controller);
    
      var results = controller.Create();
      Assert.IsNotNull(results);
      Assert.IsInstanceOf(typeof(ViewResult), results);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.