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 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

Related Questions

Using C# .NET 3.5 and WCF, I'm trying to write out some of the
Using C#, I need a class called User that has a username, password, active
I am attempting to pull some information from my tnsnames file using regex. I
Using the C# Facebook SDK 5.0.3 everything works fine whit the client.Get(/me). But when
Using the Sanitize gem, I'm cleaning some HTML. In the href attribute of my
Using CouchDB 1.0.1. I have DELETEd some documents, then I PUT some other documents
Using Maven to build my project under windows works fine, but when I build
Using Moq, RhinoMocks or a similar framework, is there a way to configure a
Using online interfaces to a version control system is a nice way to have
Using PyObjC , you can use Python to write Cocoa applications for OS X.

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.