asp.net – .Net Core GetUserAsync Identity Test

Using .NET Core 2.2, I try to test a Razor page with the help of Identity Framework which starts like this

var user = wait _userManager.GetUserAsync (user);

There are many online solutions that can be summarized by this generic class: configure UserManager on an IUserStore mock and configure the user on PageContext.

Public class SetupContext where T: IdentityUser, New ()
{
public Mock <IUserRoleStore> MockUserStore {get; private ensemble; }

public UserManager SetupUserManager ()
{
MockUserStore = new Mock <IUserStore> () Like <IUserRoleStore> ();
MockUserStore.Setup (x => x.FindByIdAsync (It.IsAny(), Ça.IsAny())). ReturnsAsync (
(String ID, CancellationToken token) => new T ()
{
Id = Guid.Parse (id),
UserName = "User Name"
});
send a new UserManager(MockUserStore.Object, null, null, null, null, null, null, null, null, null);
}

public PageContext SetupPageContext ()
{
var displayName = "User Name";
var identity = new GenericIdentity (displayName);
principle var = new ClaimsPrincipal (identity);
var httpContext = new DefaultHttpContext ()
{
User = principle
};
var modelState = new ModelStateDictionary ();
var actionContext = new ActionContext (httpContext, new RouteData (), new PageActionDescriptor (), modelState);
var modelMetadataProvider = new EmptyModelMetadataProvider ();
var viewData = new ViewDataDictionary (modelMetadataProvider, modelState);
var pageContext = new PageContext (actionContext)
{
ViewData = viewData
};
returns pageContext;
}
}

The problem is that it will not work. GetUserAsync calls GetUserId that calls

principal.FindFirstValue (Options.ClaimsIdentity.UserIdClaimType)

This claim is not configured. In fact, no ID is configured anywhere. Therefore, it can not return the user or the user's identifier. Later, I will want to access UserId database data for testing purposes.

I spent a lot of time searching and I did not find a solution. It looks like I'm 90%, but what else do I need to configure the test properly?

The test I'm going to run will be configured like this

Private async task SetupModelAsync ()
{
_db = new RemoteHealingTechDbContext (InMemoryDbContext.GetTestDbOptions ());
wait for SeedData.Initialize (_db);
var _setup = new SetupContext();
var userManager = _setup.SetupUserManager ();
var pageContext = _setup.SetupPageContext ();
return the new FreeTrialModel (_db, userManager)
{
PageContext = pageContext
};
}