Created
March 28, 2018 15:24
-
-
Save Alexei000/f4c7046fa78d15aea9064fb81d35c5fb to your computer and use it in GitHub Desktop.
Fake current user
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void FakeCurrentUser(int userId) | |
{ | |
var userRef = DataAccess.AppUserRepository.AllNoTracking.FirstOrDefault(u => u.AppUserId == userId); | |
var securitySubstitude = Substitute.ForPartsOf<SecurityService>( | |
Kernel.Get<IScopedDataAccess>(), | |
Kernel.Get<IMappingService>(), | |
Kernel.Get<ILoggingService>(), | |
Kernel.Get<IEnvironmentProxyService>()); | |
// ReSharper disable once ReturnValueOfPureMethodIsNotUsed | |
securitySubstitude.When(x => x.GetUsername()).DoNotCallBase(); | |
securitySubstitude.GetUsername().Returns(userRef?.Username ?? "<none>"); | |
// ReSharper disable once ReturnValueOfPureMethodIsNotUsed | |
securitySubstitude.When(x => x.GetCurrentUserId()).DoNotCallBase(); | |
securitySubstitude.GetCurrentUserId().Returns(userId); | |
Kernel.Rebind<ISecurityService>().ToConstant(securitySubstitude); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment