lundi 12 avril 2021

How to mock GenerateChangeEmailTokenAsync in ASP.NET

I’m trying to mock GenerateChangeEmailTokenAsync for tests. Here is my code

userManagerMock = new Mock<ExtendedUserManager>(
                    userStore.Object,
                    new Mock<IOptions<IdentityOptions>>().Object,
                    new Mock<IPasswordHasher<User>>().Object,
                    new IUserValidator<User>[0],
                    new IPasswordValidator<User>[0],
                    new Mock<ILookupNormalizer>().Object,
                    new Mock<IdentityErrorDescriber>().Object,
                    new Mock<IServiceProvider>().Object,
                    new Mock<ILogger<UserManager<User>>>().Object);

userManagerMock.Setup(u => u.GenerateChangeEmailTokenAsync(It.IsAny<User>(), It.IsAny<String>())).ReturnsAsync("TokenIsHere");

But my code returns "null". Here is the Controller

 public async Task<IActionResult> Register([FromBody] UserViewModel model)
        {
            _logger.LogDebug("Register new user");

            var user = mapper.Map<User>(model);
            user.Company.Active = false;

            var result = await userManager.CreateAsync(user, model.Password);

            if (result.Succeeded)
            {
                await userManager.AddToRoleAsync(user, Roles.NO_ACCESS);

                //send confirmation email

                string confirmationToken = userManager.GenerateEmailConfirmationTokenAsync(user).Result;

                HostString hostString = HttpContext.Request.Host;

                this.mailSender.SendConfirmationMailAsync(user, hostString, confirmationToken);

                return Ok();
            }
            else
            {
                _logger.LogInformation("User could not be registered Errors:");
                result.Errors.ToList().ForEach(e => _logger.LogInformation(e.Description));

                return BadRequest(result.Errors);
            }

        }

I didn't find anything about it therefore it would be nice if anyone could help me. Thanks in advance

Aucun commentaire:

Enregistrer un commentaire