0

ユーザーが失敗して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;
    }
4

2 に答える 2

1

わかりました、今私はあなたがやりたいことを正確に手に入れました。編集前に書いたことは無視してください。

Dim logInMock As New Moq.Mock(Of IUserLoginRepository)()

Dim IsAccountLocked = false

logInMock.SetupGet(Function(repo) repo.IsAccountLocked).Returns(Function() { return isAccountLocked })


dim amountOfFailedlogIns = 0;
logInMock.Setup(Function(repo) repo.LogInUser(It.IsAny(Of String), It.IsAny(Of String))).Callback(Function(repo) amountOfFailedlogIns++).Returns(False)

logInMock.Setup(Function(repo) repo.FailedAttempts(It.IsAny(Of String))).Returns(Function(repo) return amountOfFailedlogIns)

C# の場合:

Mock<IUserLogicRepository> logInMock = new Mock<IUserLoginRepository>();

bool IsAccountLocked = false

logInMock.SetupGet(repo => repo.IsAccountLocked).Returns(() => { return isAccountLocked; })


int amountOfFailedlogIns = 0;
logInMock.Setup(repo => repo.LogInUser(It.IsAny<string>(), It.IsAny<string>()))).Callback(repo => { amountOfFailedlogIns++; }).Returns(false);

logInMock.Setup(repo => repo.FailedAttempts(It.IsAny<string>()).Returns(repo => { return amountOfFailedlogIns; })
于 2012-06-24T05:49:35.700 に答える
0

それLoginRepoがあなたが嘲笑したいことのようです。すでに 2 回ログインに失敗しているユーザーを返すようにモック リポジトリを設定し、不正な資格情報で LoginBo を呼び出し、アカウントがモック リポジトリでロックされていることを確認します。

明確にするために、ログインに 3 回失敗する状況をテストするには、LoginBo を 3 回呼び出しません。すでに失敗の準備ができているセットアップで一度呼び出します。

于 2012-06-24T21:27:18.540 に答える