2012-06-24 32 views
0

我有問題設置模擬失敗,如果用戶嘗試登錄三次失敗。我的代碼如下所示:MoQ如何設置多個調用相同的方法

<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 ;-)

的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; 
    } 

回答

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; }) 
+0

嗨eyossi,謝謝你的帖子。這似乎並不正確。 logInBo.Setup? loginBo是我的課程,我不想嘲笑它。我想要的是,如果用戶嘗試3次登錄並失敗,則調用Repository LockAccount。我應該能夠驗證LockAccount被調用並且AccountIsLocked爲真。你可以請更新你的代碼,澄清事情?我只是想在我的上面的測試函數中填充「TODO」thingo的一些代碼。 – user1477660

+0

誰調用LockAccount方法? logInBO?誰更改了「IsAccountLocked」值? (我想這裏是實現IUserLoginRepository本身的對象) – eyossi

+0

好吧,它是LogInBo獲取LogInRepository作爲我試圖模擬的構建依賴項。 ... LoginBo中的代碼如下所示: – user1477660

0

在我看來,那是LoginRepo要嘲笑什麼。你設置你的模擬回購,以便它返回一個已經失敗了兩次的用戶,然後用錯誤的憑據調用LoginBo,然後驗證該賬戶已被鎖定在Mock倉庫中。

爲了澄清,爲了測試3次登錄失敗的情況,您不要調用LoginBo 3次;你用一個已經準備好失敗的設置調用它。

相關問題