2016-03-22 23 views
1

私はASP.NET MVC 4でアプリケーションを開発しています。私はTDDアプローチを使用してアプリケーションを開発しています。最初は、アプリケーションのログインモジュールを実装しようとしています。技術的にログインするには、次の手順を実行する必要があります。MVC複数の依存関係

  1. ユーザーアカウントがロックされておらず、有効なユーザーであることを確認してください。 (ユーザーが複数回ログインしようとすると、失敗した5回の試みの後でアカウントをロックする必要があります)これを達成するには、データベースにLoginAttemptフィールドがあります。第三者サービスを使用するloginIdとパスワードを持つユーザー。
  2. 確認された場合は、ユーザーを[インデックス]ページにリダイレクトする必要があります。

これらのタスクを達成するために、私が作成している:

// Interface, Controller will interact with  
public Interface IAuthenticate 
{ 
    bool ValidateUser(string UserId,string Password); 
} 

// Class that implement IAuthenticate 
public class Authenticate : IAuthenticate 
{ 

    private IVerifyUser loginVerify; 
    private IThirdPartyService thirdpartyService; 

    public Authenticate(IVerifyUser user,IThirdPartyService thirdparty) 
    {  
     this.loginVerify=user; 
     this.thirdpartyService=thirdparty;  
    } 

    public bool ValidateUser(string userId,string password) 
    { 
     if(loginVerify.Verify(userId)) 
     { 
      if(thirdpartyService.Validate(userId,password)) 
       return true; 
      else 
       return false;  
     } 
     else 
      return false; 
    } 
} 

を私のコントローラのログインをテストするために、私はちょうどIAuthenticateのモックを作成しなければならないのか、私はIVerifyUserIThirdPartyServiceのためのモックを作成する必要がありますか? ?

[TestMethod] 
public void Login_Rerturn_Error_If_UserId_Is_Incorrect() 
{ 
    Mock<IAuthenticate> mock1 = new Mock<IAuthenticate>(); 

    mock1.Setup(x => x.ValidateUser("UserIdTest", "PasswordTest")) 
     .Returns(false); 

    var results = controller.Login(); 
    var redirect = results as RedirectToRouteResult; 

    Assert.IsNotNull(results); 
    Assert.IsInstanceOfType(results, typeof(RedirectToRouteResult)); 

    controller.ViewData.ModelState.AssertErrorMessage("Provider", "User Id and Password is incorrect"); 

    Assert.AreEqual("Index", redirect.RouteValues["action"], "Wrong action"); 

    Assert.AreEqual("Home", redirect.RouteValues["controller"], "Wrong controller"); 
} 

本当にありがとうございますか?

答えて

1

コントローラーをテストしていて、コントローラーがインスタンスIAuthenticateに依存している場合は、それがあなたのモックインする必要があります。それを嘲笑することで、あなたはその中の実際の実装を無視しています。 IAuthenticateを使用して終了動作が発生した場合にのみ、コントローラの動作をテストしています。あなたのユニットテストIAuthenticateの実装のためのテストあなたは、それが彼らの方法のいずれかから特定の最終結果を与えられたどのように動作するかをテストするためにその依存関係(IVerifyUserIThirdPartyService)をあざけるだろうに

説明が必要な場合は、ご意見ください。 :)

+0

詳細を教えてください。どのように私はそれを行うだろう、すべてのガイドラインは本当に感謝されます。 – Faisal

+0

また、IVerifyUserとIThirdPartyServiceをテストする必要がある場合はどうすればよいですか? – Faisal

関連する問題