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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:47:52+00:00 2026-05-25T21:47:52+00:00

Right now I’m trying to test Session variables. For example I set: Session[test] =

  • 0

Right now I’m trying to test Session variables. For example I set:

Session["test"] = "test";

Then:

if (blah) { Session["test"] = "foo"; }
else if (testing) { Session["test"] = "foo2"; }

I’d like to be able to run tests so that depending on the conditions I set forth I can see that the Session variable has changed appropriately. I’ve tried using Moq and I’m able to create a Mock Session and then define a variable to return something specific:

controllerContext.Setup(x => x.HttpContext.Session["test"]).Returns("test");

This doesn’t change though when running through the test.
It seems even further complicated if I’m strongly typing the variables by using a Session wrapper since I try to pass the mock session and context between controllers.

I’ve a read a little about FakeContext and Fake Sessions as opposed to Mock. I’ve also heard a little about using delegates. Really I’m just trying to emulate what happens when the code is actually running and make sure the end result is what I expect and it happens to often depend on my Session variables. What would be the best approach towards testing their modification is properly being done?

  • 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-25T21:47:52+00:00Added an answer on May 25, 2026 at 9:47 pm

    It’s good to see that you have used Moq, in which case instead of asserting the value of the session you can assert if the code has attempted to set the session.. Asserting on behaviour basically..

    Here are a couple of useful links:

    ASP.NET MVC Unit Testing – Sessions

    ASP.NET MVC Tip #12 – Faking the Controller Context

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

Sidebar

Related Questions

Right now, I'm trying to separate out a set of constantly changing classes into
Right now i'm trying to get this: Array ( [0] => hello [1] =>
Right now i'm on testing something in a database. It's a wordpress database. i
Right now I'v created a polygon, then I do the same thing but with
Right now I am trying to use selenium ide to write code that makes
Right now when i do something like this: use strict; use warnings; package My::Test;
Right now I am trying to save a variable when i close the app
Right now I'm asking the user for two numbers. I'm trying to print the
Right now I'm stuck between using PHP's native session management, or creating my own
Right now, you have to use fakeTagsActivity ( http://developer.android.com/resources/samples/NFCDemo/src/com/example/android/nfc/simulator/FakeTagsActivity.html ) if you want to

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.