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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T08:39:52+00:00 2026-06-10T08:39:52+00:00

I am trying to unit test a razor view (MVC .NET). I want to

  • 0

I am trying to unit test a razor view (MVC .NET). I want to test whether the user logged in’s name appears on the site. I am not sure how will I do this due to User.Identity.Name – this is a static object, so can’t be mocked.

Should this static object be set in the test? Is there a nicer way of doing this?

Thanks!

  • 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-10T08:39:54+00:00Added an answer on June 10, 2026 at 8:39 am

    You could create create a ControllerContext with a mock HttpContext to return an IPrincipal to your liking. See this post on how to do that:

    Mocking and HttpContextBase.get_User()

    The example uses Rhino Mocks, but is easily adaptable to whatever mock framework you are using. Make sure that the fake IPrincipal.Identity.Name returns a string for the name. I can give you an example with FakeItEasy or Rhino Mocks if you think its not clear.

    You can then assert that the user name placed in the ViewBag or in the ViewModel for your view is equal to the name you let your fake IIdentity return.

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

Sidebar

Related Questions

I'm trying to unit test a page in my ASP.NET MVC application that, when
I'm trying to unit test my websocket on node.js and want to mock out
I am new to test driven development, and trying to unit test an mvc
I'm trying to unit test a few .NET classes that (for good design reasons)
I've run into a bit of an issue trying to unit test an MVC
I am trying to unit test an action filter I wrote. I want to
I'm trying to unit test a custom model binder - specifically, I want to
When trying to unit test some part of my code, I need a user
What I want to do I'm trying to write unit test to check that
I'm trying to unit test struts2 actions (hitting a real database), but I haven't

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.