如果用户尝试登录三次失败,我将无法设置模拟失败。我的代码如下所示:
<TestMethod()>
Public Sub User_LogIn_With_Three_Failed_Attempts_AccountLocks_Pass()
' arrange
Dim logInMock As New Moq.Mock(Of IUserLoginRepository)()
logInMock.Setup(Function(repo) repo.LogInUser(It.IsAny(Of String), It.IsAny(Of String))).Returns(False)
' Todo: have to instruct the mock that if it is repeated 3 times then lock the account. How to do this????
' act
Dim logInBO As New LogInBO(logInMock.Object)
logInBO.LogIn("someusername", "someWrongPassword")
logInBO.LogIn("someusername", "someWrongPassword")
logInBO.LogIn("someusername", "someWrongPassword")
' verify
logInMock.Verify(Function(r) r.LogInUser("someusername", "someWrongPassword"), times:=Times.Exactly(3))
logInMock.Verify(Function(r) r.IsAccountLocked = True)
End Sub
假设我的存储库看起来像:
public interface IUserLoginRepository
{
bool LogInUser(string userName, string password);
bool IsAccountLocked { get; }
bool ResetPassword(string userName, string password);
int FailedAttempts(string userName);
bool LockAccount(string userName);
}
感谢任何提示,并且会喜欢 VB.Net 中的 moq 语法;-)
LoginBo 代码如下:
public bool LogIn(string userName, string password)
{
if (_logInRepo.IsAccountLocked)
// TODO log error
return false;
if (_logInRepo.LogInUser(userName, password))
{
return true;
}
else
{
if (_logInRepo.FailedAttempts(userName) == 3) // this should increment the failed attempts and return the value
_logInRepo.LockAccount(userName);
}
// Log error
return false;
}