I have ChangePassword method where I have UserManager.ChangePasswordAsync(userId, oldPassword, newPassword); to change password'.
Problem: Above line gives the following error.
System.NotSupportedException: Store does not implement IUserPasswordStore.
ChangePassword method line of code
IdentityResult result = await UserManager.ChangePasswordAsync(userId, oldPassword, newPassword);
Unit Test
var mock = new Mock<MyController>();
mock.CallBase = true;
var obj = mock.Object;
obj.ControllerContext = new HttpControllerContext { Request = new HttpRequestMessage() };
obj.Request.SetOwinContext(CommonCodeHelper.mockOwinContext());
IPrincipal user = GetPrincipal();
obj.ControllerContext.RequestContext.Principal = user;
var result = await obj.ChangePassword(dto);
IOwinContext mocking code
public static IOwinContext mockOwinContext()
{
var owinMock = new Mock<IOwinContext>();
owinMock.Setup(o => o.Authentication.User).Returns(new ClaimsPrincipal());
owinMock.Setup(o => o.Request).Returns(new Mock<OwinRequest>().Object);
owinMock.Setup(o => o.Response).Returns(new Mock<OwinResponse>().Object);
owinMock.Setup(o => o.Environment).Returns(new Dictionary<string, object> { { "key1", 123 } });
var traceMock = new Mock<TextWriter>();
owinMock.Setup(o => o.TraceOutput).Returns(traceMock.Object);
var userStoreMock = new Mock<IUserStore<IfsUser>>();
userStoreMock.Setup(s => s.FindByIdAsync("User1@ifstoolsuite.com")).ReturnsAsync(new ApplicationUser
{
Id = "User1@test.com",
FirstName = "Test",
LastName = "User1",
Email = "User1@test.com",
UserName = "User1@test.com",
});
var appManager = new ApplicationUserManager(userStoreMock.Object);
owinMock.Setup(o => o.Get<ApplicationUserManager>(It.IsAny<string>())).Returns(appManager );
return owinMock.Object;
}
After getting above error, i tried to mock IUserPassword inside IOwinContext code block (above method) using following line of code
var passwordManager = userStoreMock.As<IUserPasswordStore<ApplicationUser>>();
I think the above mocking code for IUserPassword isn't correct becuase now it gives exception
System.InvalidOperationException: UserId not found.
Any help how do i mock IUserPasswordStore<TUser>?
Aucun commentaire:
Enregistrer un commentaire