私はASP.NET MVC 4でアプリケーションを開発しています。私はTDDアプローチを使用してアプリケーションを開発しています。最初は、アプリケーションのログインモジュールを実装しようとしています。技術的にログインするには、次の手順を実行する必要があります。MVC複数の依存関係
- ユーザーアカウントがロックされておらず、有効なユーザーであることを確認してください。 (ユーザーが複数回ログインしようとすると、失敗した5回の試みの後でアカウントをロックする必要があります)これを達成するには、データベースに
LoginAttempt
フィールドがあります。第三者サービスを使用するloginIdとパスワードを持つユーザー。 - 確認された場合は、ユーザーを[インデックス]ページにリダイレクトする必要があります。
これらのタスクを達成するために、私が作成している:
// 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
のモックを作成しなければならないのか、私はIVerifyUser
とIThirdPartyService
のためのモックを作成する必要がありますか? ?
[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");
}
本当にありがとうございますか?
詳細を教えてください。どのように私はそれを行うだろう、すべてのガイドラインは本当に感謝されます。 – Faisal
また、IVerifyUserとIThirdPartyServiceをテストする必要がある場合はどうすればよいですか? – Faisal