Search code examples
.netasp.net-mvcasp.net-identityclaims-based-identityclaims

Is there any way I can mock a Claims Principal in my ASP.NET MVC web application?


I've got some ASP.NET MVC controller code that checks if a user is authenticated and if so, it checks to see if it has a specific claim. Works fine.

I've got some unit tests and I need to mock out an IPrincipal (which is easy to do) ... but I'm not sure how to check for the claims! I usually do something like

public static ClaimsPrincipal ClaimsPrincipal(this Controller controller)
{
    return controller.User as ClaimsPrincipal;
}

and some controller code ...

this.ClaimsPrincipal().HasClaim(x => x.......);

but this all fails when I test this in my Unit Test .. because I'm not sure how I can mock the ClaimsPrincipal

Any ideas?


Solution

  • Also most of the methods are virtual so those are mock-able.