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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:24:28+00:00 2026-06-16T01:24:28+00:00

When writing a unit test for an action that uses User.Identity, shows an error

  • 0

When writing a unit test for an action that uses User.Identity, shows an error Unable to cast object of type ‘Castle.Proxies.IIdentityProxy’ to type ‘MyApp.Web.Models.CurrentUser’.

Current user class :

[Serializable]
public class CurrentUser : IIdentity
{
    public CurrentUser (){}
    public CurrentUser (string name, string displayName, int userId)
    {
        this.Name = name;
        this.DisplayName = displayName;
        this.UserId = userId;
    }
    public CurrentUser (string name, string displayName, int userId,string roleName)
    {
        this.Name = name;
        this.DisplayName = displayName;
        this.UserId = userId;
        this.RoleName = roleName;
    }
    public CurrentUser (string name, UserInfo userInfo)
        : this(name, userInfo.DisplayName, userInfo.UserId,userInfo.RoleName)
    {
        if (userInfo == null) throw new ArgumentNullException("userInfo");
        this.UserId = userInfo.UserId;
    }

    public CurrentUser (FormsAuthenticationTicket ticket)
        : this(ticket.Name, UserInfo.FromString(ticket.UserData))
    {
        if (ticket == null) throw new ArgumentNullException("ticket");
    }

    public string Name { get; private set; }

    public string AuthenticationType
    {
        get { return "GoalSetterForms"; }
    }

    public bool IsAuthenticated
    {
        get { return true; }
    }

    public string DisplayName { get; private set; }
    public string RoleName { get; private set; }
    public int UserId { get; private set; }
}

Controller Action

public PartialViewResult SomeList()
{
    int userid = ((CurrentUser )(User.Identity)).UserId; //Error shows here

    var list= listService.GetLists(userid);
    return PartialView("ListView", list);
}

Unit Test

[Test]
public void SomeList_Test()
{

    var controllerContext = new Mock<ControllerContext>();
    var principal = new Moq.Mock<IPrincipal>();

    ListController controller = new ListController (listService);

    principal.SetupGet(x => x.Identity.Name).Returns("abc");
    controllerContext.SetupGet(x => x.HttpContext.User).Returns(principal.Object);
    controllerContext.SetupGet(p =>      p.HttpContext.Request.IsAuthenticated).Returns(true);
    controller.ControllerContext = controllerContext.Object;

}

Thanks in advance,
Razack

  • 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-16T01:24:29+00:00Added an answer on June 16, 2026 at 1:24 am

    I just ran into this same thing but a little different.

    replace

    principal.SetupGet(x => x.Identity.Name).Returns("abc");
    

    with

    principal.SetupGet(x => x.Identity).Returns(new CurrentUser("abc", "test", 123));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a unit test for a Text Scrubber utility that will remove any
I am writing a unit test that needs to access an image file that
I am having some trouble with writing a unit test that checks my custom
I am writing a c# unit test that validates string properties for an ORM
We are writing unit tests for our ASP.NET application that run against a test
I am currently writing unit tests for a ViewModel in my project that uses
I'm writing a unit test and I call an action method like this var
How effective is the CXX test framework, given that you are writing unit test
I'm writing a unit test for a custom model binder (MVC 3) and want
I am writing a unit test and the controller method is throwing an exception

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.