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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:37:28+00:00 2026-05-26T11:37:28+00:00

In my simple Index() ActionMethod I reference the User.Identity property. So, I thought that

  • 0

In my simple Index() ActionMethod I reference the User.Identity property. So, I thought that I need to mock that.

So i create a some mock HomeController and use that in my unit test. When I do that, the ActionMethod returns null as the view. When I replace the mock’d controller with a concrete instance (and of course comment out any reference to User.Identity) then the correct view is returned.

eg.

// Arrange.
var homeController = Mock<HomeController>(..);
homeController.Setup(x => x.User).Returns(new GenericPrincipal(..));

// Act.
var result = homeController.Index();

// Assert.
Assert.IsNotNull(result); // This fails here. result is NULL.

but when I do this (and comment out any User reference), it works…

// Arrange.
var homeController = new HomeController(..);

// Act.
var result = homeController.Index();

// Assert.
Assert.IsNotNull(result); // Tick!

Any ideas why this is?

  • 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-26T11:37:28+00:00Added an answer on May 26, 2026 at 11:37 am

    I think you should be mocking a HttpContext for the controller to work with. I provided one on another answer that you could use. As Steve Rowbotham says, you should be mocking the dependencies of the system under test (i.e. the dependencies of the controller) and not mocking the system under test itself; you want to test the real version of the controller not a mock 🙂

    Using the HttpContextBase class from the link, you would simply do the following in your test

    var controllerToTest = new HomeController();
    var context = new MockHttpContextBase(controllerToTest);
    
    // do stuff that you want to test e.g. something goes into session
    
    Assert.IsTrue(context.HttpContext.Session.Count > 0); 
    

    You may go a step further and create Setup and TearDown methods to set the controller up with a mocked context.

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

Sidebar

Related Questions

Let's create a very simple example file: index.php : <?php $connection = mysqli_connect('localhost', 'user',
I have a simple lucene index, that contains some demo documents: Title, Keywords, H1Tag
I have created a simple application and edited the index.erb file so that I
I have a simple index.php file which checks for user authentication, and if all
I have a simple jQueryMobile index.html page. Within that I have two pages using
I have a simple page (index.html) with a div: I am populating that div
If you do a simple index into Request's items via Request[key], it looks in
Is there a simple way to get array of all index paths for the
I have got a simple MySQL table and primary index (id) is not numbered
Does anyone know of a simple way to set a selected index or item

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.