Open Closed

Mocking CurrentUser for tests. #6621


User avatar
0
SamirBoutazzout created

Hello everyone,

I would like to get some information regarding mocking the CurrentUser instance for tests. I have tried this implementation that I found on google:

protected override void AfterAddApplication(IServiceCollection services) { currentUser = Substitute.For<ICurrentUser>(); services.AddSingleton(currentUser); }

I put this in my test class and when I debug it seems to work, but when the executing code reaches the CurrentUser instance in my application service, it throws null exception as shown below.!

  • ABP Framework version: v6.0.2 Commercial
  • UI Type: Angular
  • Database System: EF Core (PostgreSQL)
  • Tiered (for MVC) or Auth Server Separated (for Angular): yes
  • Exception message and full stack trace: System.NullReferenceException: 'Object reference not set to an instance of an object.'
  • Steps to reproduce the issue:

3 Answer(s)
  • User Avatar
    0
    Anjali_Musmade created
    Support Team Support Team Member

    Hi ,

    Can you share error logs .

    Thanks,

  • User Avatar
    0
    SamirBoutazzout created

    Do you mean the stacktrace of the exception? Nothing is logged in the log file when the test throws the exception.

  • User Avatar
    0
    Anjali_Musmade created
    Support Team Support Team Member

    Yes, please send a stack trace if any

Made with ❤️ on ABP v8.2.0-preview Updated on March 25, 2024, 15:11