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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T14:28:34+00:00 2026-05-11T14:28:34+00:00

I am using shanselmann’s MvcMockHelper class to mock up some HttpContext stuff using Moq

  • 0

I am using shanselmann’s MvcMockHelper class to mock up some HttpContext stuff using Moq but the issue I am having is being able to assign something to my mocked session object in my MVC controller and then being able to read that same value in my unit test for verification purposes.

My question is how do you assign a storage collection to the mocked session object to allow code such as session[‘UserName’] = ‘foo’ to retain the ‘foo’ value and have it be available in the unit test.

  • 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. 2026-05-11T14:28:35+00:00Added an answer on May 11, 2026 at 2:28 pm

    I started with Scott Hanselman’s MVCMockHelper, added a small class and made the modifications shown below to allow the controller to use Session normally and the unit test to verify the values that were set by the controller.

    /// <summary> /// A Class to allow simulation of SessionObject /// </summary> public class MockHttpSession : HttpSessionStateBase {     Dictionary<string, object> m_SessionStorage = new Dictionary<string, object>();      public override object this[string name]     {         get { return m_SessionStorage[name]; }         set { m_SessionStorage[name] = value; }     } }  //In the MVCMockHelpers I modified the FakeHttpContext() method as shown below public static HttpContextBase FakeHttpContext() {     var context = new Mock<HttpContextBase>();     var request = new Mock<HttpRequestBase>();     var response = new Mock<HttpResponseBase>();     var session = new MockHttpSession();     var server = new Mock<HttpServerUtilityBase>();      context.Setup(ctx => ctx.Request).Returns(request.Object);     context.Setup(ctx => ctx.Response).Returns(response.Object);     context.Setup(ctx => ctx.Session).Returns(session);     context.Setup(ctx => ctx.Server).Returns(server.Object);      return context.Object; }  //Now in the unit test i can do AccountController acct = new AccountController(); acct.SetFakeControllerContext(); acct.SetBusinessObject(mockBO.Object);  RedirectResult results = (RedirectResult)acct.LogOn(userName, password, rememberMe, returnUrl); Assert.AreEqual(returnUrl, results.Url); Assert.AreEqual(userName, acct.Session['txtUserName']); Assert.IsNotNull(acct.Session['SessionGUID']); 

    It’s not perfect but it works enough for testing.

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

Sidebar

Ask A Question

Stats

  • Questions 103k
  • Answers 103k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Try something like the following: protected static IndexSearcher searcher =… May 11, 2026 at 8:25 pm
  • Editorial Team
    Editorial Team added an answer Look into extract(). You can do something like this: <?php… May 11, 2026 at 8:25 pm
  • Editorial Team
    Editorial Team added an answer Probably the easiest way is: SELECT EXTRACT(epoch FROM my_interval)/3600 May 11, 2026 at 8:25 pm

Related Questions

I am using shanselmann's MvcMockHelper class to mock up some HttpContext stuff using Moq
I am using SQL Server 2005. I have a table with a text column
I am using LINQ to query a generic dictionary and then use the result
I am using Hibernate in a Java application to access my Database and it
I am using MSBuild to build my stuff. I want to use CruiseControl.net as

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.