Mock session in NUnit tests

I have one method inside my code which is fetching session and using the UserAuthId in further process

 public async Task<object> PostAsync(RequestDTO request)
 {
     var session = await GetSessionAsync();
     var userAuthId = session.UserAuthId;
    
     // Rest code
}

Now I want to write unit tests for this and I am able to mock rest dependency but every time I am getting session as null

I am mocking UserManager and EmailSender which is needed for my methods

 [SetUp]
 public void SetUp()
 {
     _appHost = new BasicAppHost().Init();

     var container = _appHost.Container;
     _userManagerMock = new Mock<UserManager<ApplicationUser>>(
             Mock.Of<IUserStore<ApplicationUser>>(), null!, null!, null!, null!, null!, null!, null!, null!);

     _emailSenderMock = new Mock<IEmailSender<ApplicationUser>>();

     _appSettingsMock = new Mock<IAppSettings>();

     container.Register(_userManagerMock.Object);
     container.Register(_emailSenderMock.Object);
     container.Register(_appSettingsMock.Object);
     _target = container.Resolve<MyServices>();
 }

So how will I mock session to lets say return specific details for eg: UserAuthId to be “user1”

I got the solution,

I created a BasicRequest and added a session variable inside it which helped me to get a dummy session.

Thanks!

I avoid Mocks, prefer using real types instead. But when you’re running an AppHost with TestMode=true enabled (e.g. implicitly set when using BasicAppHost), you can register your session in your IOC against IAuthSession, e.g:

container.Register<IAuthSession>(new AuthUserSession());

Alternatively when you have access to the Service instance or IRequest context you can set the session with:

service.Request.Items[Keywords.Session] = new AuthUserSession();

Okay thanks for the information about testmode