Skip to content

Instantly share code, notes, and snippets.

@Alexei000
Created March 28, 2018 15:24
Show Gist options
  • Save Alexei000/f4c7046fa78d15aea9064fb81d35c5fb to your computer and use it in GitHub Desktop.
Save Alexei000/f4c7046fa78d15aea9064fb81d35c5fb to your computer and use it in GitHub Desktop.
Fake current user
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