How to simulate Asp.net UserManager identifier CreateAsync method

I am trying to make fun of the UserStore CreateAsync method for storing in user memory.

 public void Can_Create_User() { //Arrange var dummyUser = new ApplicationUser() { UserName = "PinkWarrior", Email = " PinkWarrior@PinkWarrior.com " }; var mockStore = new Mock<IUserStore<ApplicationUser>>(); var userManager = new UserManager<ApplicationUser>(mockStore.Object); mockStore.Setup(x => x.CreateAsync(dummyUser)) .Returns(Task.FromResult(IdentityResult.Success)); //Act Task<IdentityResult> tt = (Task<IdentityResult>)mockStore.Object.CreateAsync(dummyUser); var user = userManager.FindByName("PinkWarrior"); //Assert Assert.AreEqual("PinkWarrior", user.UserName); } 

user above is always null because I never insert it. So the problem I have is , how can I make a userManager that is not a false return of the user from my store layout?

This answer was somewhat similar, but does not answer my question here.

Any help is much appreciated

+5
source share
3 answers

You need to make fun of the call to FindByNameAsync because this will be called by UserManager:

 public void Can_Create_User() { //Arrange var dummyUser = new ApplicationUser() { UserName = "PinkWarrior", Email = " PinkWarrior@PinkWarrior.com " }; var mockStore = new Mock<IUserStore<ApplicationUser>>(); var userManager = new UserManager<ApplicationUser>(mockStore.Object); mockStore.Setup(x => x.CreateAsync(dummyUser)) .Returns(Task.FromResult(IdentityResult.Success)); mockStore.Setup(x => x.FindByNameAsync(dummyUser.UserName)) .Returns(Task.FromResult(dummyUser)); //Act Task<IdentityResult> tt = (Task<IdentityResult>)mockStore.Object.CreateAsync(dummyUser); var user = userManager.FindByName("PinkWarrior"); //Assert Assert.AreEqual("PinkWarrior", user.UserName); } 

However, I believe that with this test you are testing Microsoft.AspNet.Identity.UserManager, not your code. What you are proving is that UserManager somehow calls your mockStore.

The source code for Microsoft.AspNet.Identity contains these types of tests.

+13
source

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); } 
+4
source

Your mock user repository is not prepared correctly. Since this is a layout, you should not expect mockStore.Object.CreateAsync to do anything, i.e. He is going to add anything anywhere.

You need to prepare the corresponding “read” operation, since most likely the user manager will call it. (set the FindByNameAsync method to the layout to return the identity you need).

In any case, what you are trying to do is pointless, as you are trying to test the UserManager class of the class.

Probably, in fact, you need to make fun of the user manager itself and use this layout in your controller or wherever you use this user manager.

+2
source

Source: https://habr.com/ru/post/1213855/


All Articles