1

Below is ChangePassword web method in AccountController API.

[HttpPost]
    [Route("changepassword")]
    public async Task<IActionResult> ChangePassword([FromBody]ChangePasswordModel model)
    {
        var user = await userManager.FindByNameAsync(this.User.Identity.Name);

        KanbanResult kanbanResult = new KanbanResult();

        if (model.NewPassword == model.ConfirmPassword)
        {
            var result = await userManager.ChangePasswordAsync(user, model.CurrentPassword, model.NewPassword );

            if (result.Succeeded)
            {
                return Ok(KanbanResult.CreateOkResult(string.Empty));
            }
            else
                return BadRequest(KanbanResult.CreateErrorResult( result.Errors.Select( er => er.Description).ToList()));

        }
        else
        {

            return BadRequest(KanbanResult.CreateErrorResult(new List<string>{"Confirmed password is not matching"}));

        }


    }

Below is my Unit test in XUnit in .NET Core 2.0

public class AccountControllerTest
{
    //AccountController accountController;

    public AccountController Setup()
    {

        var userStoreMock = new Mock<IUserStore<UserEntity>>();
        var mockUserMgr = new Mock<UserManager<UserEntity>>(userStoreMock.Object,  null, null, null, null, null, null, null, null );

        mockUserMgr.Setup(mgr => mgr.CreateAsync(It.IsAny<UserEntity>())).Returns(Task.FromResult(IdentityResult.Success));
        mockUserMgr.Setup(mgr => mgr.ChangePasswordAsync(It.IsAny<UserEntity>(), It.IsAny<string>(), It.IsAny<string>()))
                   .Returns(Task.FromResult(IdentityResult.Success));
        mockUserMgr.Setup(mgr => mgr.FindByNameAsync(It.IsAny<string>()))
                   .Returns(Task.FromResult(new UserEntity { UserName = "nityaprakash@gmail.com" }));

        var roleStoreMock = new Mock<IRoleStore<KanbanRoles>>();
        var mockRoleMgr = new Mock<RoleManager<KanbanRoles>>( roleStoreMock.Object, null, null, null, null);



        return  new AccountController(mockUserMgr.Object, mockRoleMgr.Object);
    }

    [Fact]
    public async Task Change_Password_Negative_Test()
    {

        Thread.CurrentPrincipal = new GenericPrincipal(
            new  GenericIdentity("nityaprakash@gmail.com"),
            new[] {"NormalUser"}
        );
        AccountController accountController = Setup();

         ChangePasswordModel model = new ChangePasswordModel { CurrentPassword = "currentPassowrd", ConfirmPassword = "NewPassword", NewPassword = "NewPassword" };

        var result = await accountController.ChangePassword(model);

        KanbanResult kResult = Assert.IsType<KanbanResult>(result);

        Assert.Equal("Error", kResult.Status);
    }
}

Whenever test executes ChangePassowrd method, it looks for Controller.User object, which is null when running the test. This is Asp.NET Core 2.0 API and test. How should I initialize the User or other dependent properties to run the test successfully.

Nps
  • 1,638
  • 4
  • 20
  • 40

0 Answers0