0
firnas created
AbpSession.UserId is null when running unit tests. Is there a way i can set the user id in the unit test? I am mocking my App service for testing
Markdown is supported
Copy & paste or drag & drop images (max 30 MB per image)
4 Answer(s)
-
0
Call
LoginAsDefaultTenantAdminor//Mock session Var session = Substitute.For<IAbpSession>(); session.TenantId.Returns(1); session.UserId.Returns(1); LocalIocManager.IocContainer.Register(Component.For<IAbpSession>().Instance(session));Markdown is supportedCopy & paste or drag & drop images (max 30 MB per image) -
0
Both the ways did not work.
Below is the unit testing code,
LoginAsDefaultTenantAdmin(); //arrange var mockBusinessDomainService = new Mock<IBusinessDomainService>(); mockBusinessDomainService.Setup(b => b.GetBusinessFromUser(It.IsAny<long>())) .ReturnsAsync(new Business.Business { BusinessName = "Test Business", SubscriptionStatus = Enums.SubscriptionStatus.Subscribed.ToString() }); mockBusinessDomainService.Setup(b => b.UpdateBusiness(It.IsAny<string>(), It.IsAny<Business.Business>())) .ReturnsAsync(new Business.Business { BusinessName = "Test Business", SubscriptionStatus = Enums.SubscriptionStatus.UnSubscribed.ToString() }); IBusinessAppService _businessAppService = new BusinessAppService( mockBusinessDomainService.Object, Resolve<IObjectMapper>(), Resolve<IUnitOfWorkManager>(), Resolve<SignInManager>() ); //act Task t = _businessAppService.CancelSubscription();Below is part of the method,
//check for user long userId = AbpSession.UserId.GetValueOrDefault(); if (userId == 0) { throw new UnauthorizedAccessException(); }The user id is always 0. That is the problem.
Markdown is supportedCopy & paste or drag & drop images (max 30 MB per image) -
0
Because AbpSession is property injection, it is best to use dependency injection.
If you do this directly new a BusinessAppService. You can try
_businessAppService.AbpSession = new Mock<IAbpSession>, etc.Markdown is supportedCopy & paste or drag & drop images (max 30 MB per image) -
0
Thanks for the response. I will change accordingly and test.
Markdown is supportedCopy & paste or drag & drop images (max 30 MB per image)