【发布时间】:2019-05-30 14:50:32
【问题描述】:
我创建了一个先前的测试方法,在单个数据访问上设置了两个模拟对象,它工作正常。是否有另一个具有相同场景但结果失败。
这是测试方法:
[Test]
public void UpdateUserPassword_WhenInputsAreCorrect_ReturnsQuerySuccessMessage()
{
UpdatePasswordModel input = new UpdatePasswordModel()
{
UserName = "john.doe",
NewPassword = "password1", //password1
PreviousPassword = "password" //password
};
Mock<IUserDataAccess> user = new Mock<IUserDataAccess>();
Mock<IDailyTimeInDataAccess> timeIn = new Mock<IDailyTimeInDataAccess>();
Mock<IDailyTimeOutDataAccess> timeOut = new Mock<IDailyTimeOutDataAccess>();
user.Setup(x => x.UpdatePassword(10000, input.NewPassword)).Returns("User record updated.");
user.Setup(x => x.GetUser(input.UserName)).Returns(new User()
{
UserKey = 10000,
UserName = "john.doe",
UserPassword = "LTg9BIob8urwz643K5+pBA=="
});
ILoginBusinessRules app = new LoginBusinessRules(user.Object, timeIn.Object, timeOut.Object);
var output = app.UpdateUserPassword(input);
Assert.AreEqual("User record updated.", output);
}
这是业务规则:
public string UpdateUserPassword(UpdatePasswordModel model)
{
if (model == null)
{
return "No data to process.";
}
if (string.IsNullOrEmpty(model.UserName))
{
return "Username is empty.";
}
if (string.IsNullOrEmpty(model.NewPassword))
{
return "New Password is empty.";
}
if (string.IsNullOrEmpty(model.PreviousPassword))
{
return "Previous Password is empty.";
}
var user = _userDataAccess.GetUser(model.UserName);
if (user == null)
{
return "User not found.";
}
if (user.UserPassword != EncryptPassword(model.PreviousPassword))
{
return "Previous password does not match.";
}
else
{
user.UserPassword = EncryptPassword(model.NewPassword);
user.UpdateDttm = DateTime.Now;
user.UpdateUserId = model.UserName;
var result = _userDataAccess.UpdatePassword(user.UserKey, user.UserPassword);
return result;
}
}
测试返回失败。进一步调试告诉我,这里的这一行返回 null:
var result = _userDataAccess.UpdatePassword(user.UserKey, user.UserPassword);
非常感谢任何帮助!
【问题讨论】: