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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:40:42+00:00 2026-05-23T13:40:42+00:00

I have recently Implemented a unit of work pattern, and as an environment we

  • 0

I have recently Implemented a unit of work pattern, and as an environment we are using more unit testing. Currently the implementation writes into a session helper that writes to session. How do I unit test these aspects in regard to the session? Should I make a repository pattern? (repository interface with concrete session implementation and concrete mock implementation) How is this normally done?

I know there is probably more than one way of approaching this, but I am just looking for some advice.

  • 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-23T13:40:43+00:00Added an answer on May 23, 2026 at 1:40 pm

    There are basically two ways of doing this.

    Assuming you are using .NET 3.5 or up. Change your implementation to take the HttpSessionStateBase object as a constructor parameter, you can then mock this implementation – there’s a few tutorials online on how to do this. You can then use an IoC container to wire this up at app start or do something like (poor man’s dependency injection):

    public class MyObjectThatUsesSession
    {
        HttpSessionStateBase _session;
    
        public MyObjectThatUsesSession(HttpSessionStateBase sesssion)
        {
             _session = session ?? new HttpSessionStateWrapper(HttpContext.Current.Session);
        }
    
        public MyObjectThatUsesSession() : this(null)
        {}
    }
    

    Alternatively, and probably a bit better and more flexible design would be to create a test seam by wrapping your interaction with session in another object. You could then change this to a database, cookie or cache based implementation later. Something like:

    public class MyObjectThatUsesSession
    {
        IStateStorage _storage;
    
        public MyObjectThatUsesSession(IStateStorage storage)
        {
             _storage= storage ?? new SessionStorage();
        }
    
        public MyObjectThatUsesSession() : this(null)
        {}
    
        public void DoSomethingWithSession()
        {
            var something = _storage.Get("MySessionKey");
            Console.WriteLine("Got " + something);
        }
    }
    
    public interface IStateStorage
    {
        string Get(string key);
        void Set(string key, string data);
    }
    
    public class SessionStorage : IStateStorage
    {
        //TODO: refactor to inject HttpSessionStateBase rather than using HttpContext.
    
        public string Get(string key)
        {
           return HttpContext.Current.Session[key];
        }
    
        public string Set(string key, string data)
        {
           HttpContext.Current.Session[key] = data;
        }
    }
    

    You can then use Moq to create a mock IStateStorage implementation for your tests or create a simple dictionary based implementation.

    Hope that helps.

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

Sidebar

Related Questions

I have been doing TDD and was using it more as unit testing than
I have recently started using Vim as my text editor and am currently working
I have an iPhone project that uses GHUnit to conduct unit testing. Recently, I've
I am using docmail's Simple API for sending Postcard.They have implemented this functionality recently,
I have recently started having problems with TortoiseCVS, or more specifically with plink, the
I have recently been doing a bit of investigation into the different types of
Recently have implemented lot of pages with different css classes for my web application.
I have implemented a factory in my project and it was recently suggested that
I've recently come to appreciate the copy-and-swap idiom, and have been using it to
I recently implemented a jQuery / iframe solution to file uploading and have run

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.