Check MockHelper.cs class for Identity git repo tests
Identity GitHub replication link or Closed issue for ridicule
You should find inspiration for ridicule there, for example
public static UserManager<TUser> TestUserManager<TUser>(IUserStore<TUser> store = null) where TUser : class { store = store ?? new Mock<IUserStore<TUser>>().Object; var options = new Mock<IOptions<IdentityOptions>>(); var idOptions = new IdentityOptions(); idOptions.Lockout.AllowedForNewUsers = false; options.Setup(o => o.Value).Returns(idOptions); var userValidators = new List<IUserValidator<TUser>>(); var validator = new Mock<IUserValidator<TUser>>(); userValidators.Add(validator.Object); var pwdValidators = new List<PasswordValidator<TUser>>(); pwdValidators.Add(new PasswordValidator<TUser>()); var userManager = new UserManager<TUser>(store, options.Object, new PasswordHasher<TUser>(), userValidators, pwdValidators, new UpperInvariantLookupNormalizer(), new IdentityErrorDescriber(), null, new Mock<ILogger<UserManager<TUser>>>().Object, null); validator.Setup(v => v.ValidateAsync(userManager, It.IsAny<TUser>())) .Returns(Task.FromResult(IdentityResult.Success)).Verifiable(); return userManager; }
im using it with this method:
private UserController BuildCoontrollerWithDatabase() { DbContextOptionsBuilder dbContextOptionsBuilder = new DbContextOptionsBuilder(); dbContextOptionsBuilder.UseInMemoryDatabase(); ApplicationDbContext applicationDbContext = new ApplicationDbContext( dbContextOptionsBuilder.Options); var userStore = new UserStore<ApplicationUser>(applicationDbContext); UserManager<ApplicationUser> userManager = TestUserManager<ApplicationUser>(userStore); return new UserController(userManager); }
source share