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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T05:13:17+00:00 2026-05-12T05:13:17+00:00

I just wanna ask what would be better approach to supply these objects in

  • 0

I just wanna ask what would be better approach to supply these objects in my unit tests.

In my unit test I am testing CSLA object. CSLA object is internally using one property and one method of ApplicationUser object. ApplicationUser is inherited from IPrincipal.
The properties are:
1) ApplicationContext.User.IsInRole(…) – the method is part of IPrincipal
2) ApplicationContext.User.Identity.Name – the name is property of IIdentity which is part of ApplicationUser aka IPricipal

Example of my test (using RhinoMock):

public void BeforeTest()
{
   mocks = new MockRepository();
   IPrincipal mockPrincipal = mocks.CreateMock<IPrincipal>();
   ApplicationContext.User = mockPrincipal;
   using (mocks.Record()) {
      Expect.Call(mockPrincipal.IsInRole(Roles.ROLE_MAN_PERSON)).Return(true);
      Expect.Call(mockPrincipal.Identity.Name).Return("ju"); //doesn't work!!!! return null ref exc
   }
}

I have slight problem with second value, the identity name. I tried to mock it but have problem to assign mocked IIdentity to ApplicationUser, as it is done internaly.
I was told to just create some IIPrincipal (including IIdentity) by myself and not to mock it at all. Which can be done for sure. Not sure if this can be called as Stub using?

So can you advice me how to deal with IPrincipal and IIdentity? Any suggestion most welcome.

  • 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-12T05:13:18+00:00Added an answer on May 12, 2026 at 5:13 am

    The reason you’re getting a null reference error is because IPrincipal.Identity is null; it hasn’t been set in your mocked IPrincipal yet. Calling .Name the null Identity results in your exception.

    The answer, as Carlton pointed out, is to mock IIdentity also, and set it up to return “ju” for its Name property. Then you can tell IPrincipal.Identity to return the mock IIdentity.

    Here is an expansion of your code to do this (using Rhino Mocks rather than Stubs):

    public void BeforeTest()
    {
       mocks = new MockRepository();
       IPrincipal mockPrincipal = mocks.CreateMock<IPrincipal>();
       IIdentity mockIdentity = mocks.CreateMock<IIdentity>();
       ApplicationContext.User = mockPrincipal;
       using (mocks.Record()) 
       {
          Expect.Call(mockPrincipal.IsInRole(Roles.ROLE_MAN_PERSON)).Return(true);
          Expect.Call(mockIdentity.Name).Return("ju"); 
          Expect.Call(mockPrincipal.Identity).Return(mockIdentity);
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just wanna ask if there's a way where I could put an object
wanna ask for your opinion. What would be the best object (Array, List<>, Collection,...)
I just wanna ask your opinion/suggestion on how to 'terminate' a running application/process is
I just wanna ask if there's a way in C#/ASP.NET to create 'dynamic' tables/fields
I just wanna ask if there's a possibility to retrieve the contents of a
i am using vb.net i just wanna ask if we can place a picture
Hey guys, I have some questions would like to ask: I wanna ask how
just wanna ask what is acceptable size of ViewState per page? What is the
Just wanna ask this question. Can I use detailTextLabel in a Custom Cell? I
Just wanna ask anyone here knows why when i put a placeholder in my

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.