Open Closed

How to switch inbetween ICurrentUser and their roles in Unit Tests #5839


User avatar
0
stefanos.lidas@gmail.com created
  • ABP Framework version: v7.2.3
  • UI Type: Blazor Server
  • Database System: EF Core MS SQL

I am writing unit tests for the AppService level of the project. The action I am trying to test behaves differently depending on the User roles that are assigned to ICurrentUser _currentUser. I want to test with multiple roles but I obviously can't edit my _currentUser once the test starts and from what I've gathered the _currentUser as far as the test is concerned gets set from the FakeCurrentPrincipalAccessor inside the test part of the Abp project.

My question is how can I setup multiple users with different roles and easily switch inbetween them for testing?

[Fact] public async Task Test() { //As User.... }

Markdown supported.
Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)

3 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    You can replace the FakeCurrentPrincipalAccessor in ConfigureServices method.

    https://github.com/abpframework/abp/blob/dev/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Auditing/AuditIntegrationServiceTestController_Tests.cs#L28

    Markdown supported.
    Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)
  • User Avatar
    0
    stefanos.lidas@gmail.com created

    This will change the _currentUser of every test. The crux of my question is precisely how to avoid that and instead be able to assign a User to each test separately when needed, or be able to rerun a test as a user that has different roles as shown in a tiny code snippet in my initial question.

    [Fact] public async Task Test() { //As User.... }

    What you are showing me will simply change the default test user which isn't what I want to do.

    Markdown supported.
    Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)
  • User Avatar
    1
    liangshiwei created
    Fullstack Developer

    Hi,

    You can try using the method change of ICurrentPrincipalAccessor:

    using (_currentPrincipalAccessor.Change(new[]
           {
               new Claim(AbpClaimTypes.UserId, Guid.NewGuid().ToString()),
               new Claim(AbpClaimTypes.UserName, "testUser"),
               new Claim(AbpClaimTypes.Role, "test1")
           }))
    {
        _currentUser.UserName.ShouldBe("testUser");
        _currentUser.Roles.ShouldContain("test1");
    }
    
    _currentUser.UserName.ShouldBe("admin");
    
    Markdown supported.
    Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)
Boost Your Development
ABP Live Training
Packages
See Trainings
Mastering ABP Framework Book
The Official Guide
Mastering
ABP Framework
Learn More
Mastering ABP Framework Book
Made with ❤️ on ABP v10.8.0-preview. Updated on September 24, 2026, 12:09
1
ABP Assistant
🔐 You need to be logged in to use the chatbot. Please log in first.