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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:37:10+00:00 2026-06-15T11:37:10+00:00

I wish to use unit testing in a project that I am involved in.

  • 0

I wish to use unit testing in a project that I am involved in. The solution has a UI layer, a Domain Logic Layer and Database base all seperated into physically seperated dlls, so it is pretty standard nowdays.

In the Domain layer we have a class that makes use of

new HttpContextWrapper(HttpContext.Current)

to get the context of the current request to compile a Url for dynamic menus. And as expected everything works fine when running in a web environment as this HttpContext.Current is always set.

However when I come to unit test a controller I hit this line of code and get a Null reference exception.

After some research I have many articles that suggest using Mock to create a Fake Http Context and set this when creating the controller like so:

var httpContext = FakeHttpContext();
ControllerContext context = new ControllerContext(new RequestContext(httpContext, new RouteData()), controller);
controller.ControllerContext = context;

But this still does not change the HttpContxt.Current and I am still getting a null reference exception.

What am I doing wrong / How can I resolve this exception?

  • 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-06-15T11:37:12+00:00Added an answer on June 15, 2026 at 11:37 am

    You can’t use HttpContext.Current in this way. At one of my previous gigs, in order to account for this, we had to create a separate wrapper class and access that property.

    Using statics is a very common problem in unit testing.

    Per your request for an update:

    public static class HttpContextContainer
    {
         private static HttpContext _ctx;
         public static HttpContext Current 
         {
             get 
             { 
                 if (_ctx == null) _ctx = HttpContext.Current;
                 return _ctx;
             }
             set
             {
                 _ctx = value;
             }
         }
    }
    

    Then in your usage you might do something like:

    new HttpContextWrapper(HttpContextContainer.Current)
    

    and:

    var httpContext = FakeHttpContext();
    HttpContextContainer.Current = httpContext;
    ControllerContext context = new ControllerContext(new RequestContext(httpContext, new RouteData()), controller);
    controller.ControllerContext = context;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wish to use common CRC logic in a VB.NET or C# application as
In some unit/integration tests of the code we wish to check that correct usage
I wish to unit test my business logic is loading the correct data by
Say you have a database that has served a company for 10 years. It
I wish to use .NET Regex to create a regular expression that takes an
I wish to use a smart pointer to an object that will be accessed
I wish to use Google organisational chart within my ASP.NET MVC project. I am
I wish to use the method with the following signature: exec(String command, String[] envp,
I wish to use Spring MVC to provide a REST web service. However, running
I wish to use a COM dll in my C++ library. The way I

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.