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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:29:31+00:00 2026-05-24T04:29:31+00:00

I have a Controller that call HttpContext like: [Authorize(Roles = Administrador)] public class ApuradorController

  • 0

I have a Controller that call HttpContext like:

[Authorize(Roles = "Administrador")]
public class ApuradorController : Controller
{
    private readonly Questiona2011Context _context = new Questiona2011Context();

    private readonly AuthenticationService _authenticationService = new AuthenticationService();
}

The HttpContext is call in AuthenticationService class:

public class AuthenticationService
{
    private IPrincipal _user = HttpContext.Current.User;

    ...
}

In my project where im test the controllers when I Instance the Controller a error is thrown in private IPrincipal _user = HttpContext.Current.User; Line: Object reference not set to an instance of an object.

What do I need to test my controllers?

  • 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-24T04:29:31+00:00Added an answer on May 24, 2026 at 4:29 am

    The main thing that you miss is the knowledge of how to design ASP.NET MVC project for testing.

    You should design your controller to use dependency injection. That is, controllers should not be using concrete implementation of AuthenticationService, but using IAuthenticationService, concrete implementation of which will be supplied in runtime. For now, when the controller is created, the AuthenticationService is also created. But in test scenario, HttpContext is null and Creating AuthenticationService is failing with NullReference exception. If you design that via interface, in testing purposes you will supply fake implementation of AuthenticationService to controller, and it will not throw exception.

    public interface IAuthenticationService
    {
        IPrincipal User {get;}
    }
    
    public class AuthenticationService : IAuthenticationService
    {
        private IPrincipal _user = HttpContext.Current.User;
    
        ...
    }
    
    //the controller
    [Authorize(Roles = "Administrador")]
    public class ApuradorController : Controller
    {
        private readonly Questiona2011Context _context = new Questiona2011Context();
    
        private readonly IAuthenticationService _authenticationService;
    
        public ApuradorController(IAuthenticationService authenticationService)
        {
             _authenticationService = authenticationService;
        }
    }
    

    In test scenario, you could use some mocking library for fake IAuthenticationService implementation, for example moq. And supply value for it via mocking

    var mockAuthenticationService = new Mock<IAuthenticationService>();
    //setup mockAuthenticationService
    
    var controller = new ApuradorController(mockAuthenticationService.Object);
    

    This time it will not throw exceptions.

    The information mentioned above is not helpful if you do not understand principles of unit testable project design. For the quick start, read this link. For the further reading, address books about asp.net mvc, i would reccommend those by steven sanderson. The main idea of unit testable controller design is that you should have the ability to supply fake components to the controller, fake repositories, services, etc and leave real only the part of controller that is unit tested. Then test controller iteractions with those fake parts. The unit testing means testing that interactions. If interactions are correct, they will be correct with real implementations of those components. If they’re wrong, test fails.

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

Sidebar

Related Questions

I have a controller sessions that can create session. I'd like to call it
I have multiple controller actions that takes an id public ActionResult Get(int? id) {
I have a controller that is being called twice from an ActionLink call. My
I have a Controller that I want to use for ajax scripts to call
I have an update method in my users controller that I call from a
I have a controller that handles file uploads. Ultimately I would like to be
i have a controller that have with in let's say thingy/stuff directory <?php public
Let's say I have a PHP Model-View-Controller framework that maps an address like http://example.com/admin/posts/edit/5
In Spring 3 MVC, I have a controller that I call SettingsController, and it
I have a simple Partial View that I would like to automatically call a

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.