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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:29:47+00:00 2026-06-04T13:29:47+00:00

I need to setup a policy in base controller that applies to all controller

  • 0

I need to setup a policy in base controller that applies to all controller instance, like below:

  public class BaseController : Controller
    {

        private IPolicy Policy;

        public BaseController()
        {
            this.Policy= new Policy(HttpContext);
        }
    }

Within the Policy class, I need to do something like:

   this.httpContextBase.User.

Questions: (Update)

What is the better way to design the BaseController in terms of using HttpContext and 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. Editorial Team
    Editorial Team
    2026-06-04T13:29:48+00:00Added an answer on June 4, 2026 at 1:29 pm

    What is the correct way to unit test HttpContext?

    Absolutely no way. You are using the HttpContext inside the constructor of a controller when this context is still not initialized. Not only that this code cannot be tested but when you run the application it will also crash with NRE. You should never use any HttpContext related stuff in a constructor of a controller.

    One possibility is to refactor your code and perform this inside the Initialize method:

    public class BaseController : Controller
    {
        private IPolicy Policy;
    
        protected override void Initialize(RequestContext requestContext)
        {
            base.Initialize(requestContext);
            this.Policy = new Policy(HttpContext);
        }
    }
    

    This being said, that’s not the approach I would recommend. I would recommend you using dependency injection instead of service location which is considered by many as an anti-pattern.

    So:

    public abstract class BaseController : Controller
    {
        protected IPolicy Policy { get; private set; }
    
        protected BaseController(IPolicy policy)
        {
            Policy = policy;
        }
    }
    

    Now, all that’s left is to configure your favourite Dependency Injection framework to inject the correct instance into the constructor. For example with Ninject.Mvc3 this is achieved with a single line of code:

    kernel.Bind<IPolicy>().To<Policy>();
    

    Now you can feel more than free to mock this IPolicy in your unit test without even caring about any HttpContext.

    For example let’s suppose that you have the following controller that you want to unit test:

    public class FooController : BaseController
    {
        public FooController(IPolicy policy): base(policy)
        { }
    
        [Authorize]
        public ActionResult Index()
        {
            Policy.DoSomething();
            return View();
        }
    }
    

    Now, all that you need to do is pick up your favorite mock framework (Rhino Mocks in my case) and do the mocking:

    [TestMethod]
    public void Index_Action_Should_DoSomething_With_The_Policy()
    {
        // arrange
        var policyStub = MockRepository.GenerateStub<IPolicy>();
        var sut = new FooController(policyStub);
    
        // act
        var actual = sut.Index();
    
        // assert
        Assert.IsInstanceOfType(actual, typeof(ViewResult));
        policyStub.AssertWasCalled(x => x.DoSomething());
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to setup a script in group policy that we be used in
I need to setup an ASP.Net MVC view with a Date Picker that will
I need to setup a SELECT query that will return rows based on matching
I need to setup an application on a Windows CE device that is connected
I need to setup different commands for ViewModels like Close, Edit, Cancel, ShowPic, ShowVideo.
I need to setup a Kohana dev environment that allows me to make full
I need to setup a relationship so that A has one B, but there
I need to setup a build development environment that includes the following Sonatype Nexus
I need to setup an application that watches for files being created in a
I need to setup a simple IVR system for a friend's company that will

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.