ユーザーが失敗して3回ログインしようとした場合に、モックを失敗するように設定するのに問題があります。私のコードは次のようになります:
<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;
}