Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 用于mvc登录的NUnit测试用例_Asp.net_Asp.net Mvc_Nunit - Fatal编程技术网

Asp.net 用于mvc登录的NUnit测试用例

Asp.net 用于mvc登录的NUnit测试用例,asp.net,asp.net-mvc,nunit,Asp.net,Asp.net Mvc,Nunit,在asp.net mvc中,我们可以为以下登录控制器编写什么样的测试用例 [HttpPost] [AllowAnonymous] [ValidateAntiForgeryToken] public ActionResult Login(LoginModel model, string returnUrl) { if (ModelState.IsValid && WebSecurity.Login(model.UserName, model.PasswordpersistCook

在asp.net mvc中,我们可以为以下登录控制器编写什么样的测试用例

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Login(LoginModel model, string returnUrl)
{
 if (ModelState.IsValid && WebSecurity.Login(model.UserName, model.PasswordpersistCookie: model.RememberMe))
 {
    return RedirectToLocal(returnUrl);
 }
 ModelState.AddModelError("", "The user name or password provided is incorrect.");
 return View(model);
 }
1) 当模型有效时,您可以验证是否使用正确的参数调用WebSecurity.Login

2) 当WebSecurity.Login返回true时,您可以验证重定向到reutnrUrl

3) 如果模型无效,您可以验证ModelState是否包含错误和返回的ViewResult

不过,您必须重构对WebSecurity的调用,您必须模拟它。可能必须使用依赖注入技术

示例

1) 您必须将WebSecurity隐藏在接口后面,并仅通过该接口使用它(我假设您的WebSecurity静态类来自WebMatrix.WebData命名空间)

2) 在您的控制器中,您必须创建WebSecurityHelper的实例,通常是使用IoC框架(如或)完成的。但是对于这个例子,我将在controllers构造函数中初始化websecurityhelp,这仍然允许我注入mock进行测试

public class LoginController
{
   private readonly IWebSecurityHelper _helper;

   public LoginController(IWebSecurityHelper helper)
   {
     _helper = helper;
   }

   // passing your implementation of the WebSecurityHelper 
   public LoginController() : this(new WebSecurityHelper()) {}

   [HttpPost]
   [AllowAnonymous]
   [ValidateAntiForgeryToken]
   public ActionResult Login(LoginModel model, string returnUrl)
   {
       if (ModelState.IsValid && _helper.Login(model.UserName,model.PasswordpersistCookie: model.RememberMe))
       {
         return RedirectToLocal(returnUrl);
       }

       ModelState.AddModelError("", "The user name or password provided is incorrect.");

       return View(model);
   }      
}
3) 在单元测试中,必须模拟IWebSecurityHelper。我个人更喜欢很多模拟框架

[TestFixture]
公共类登录控件测试
{
//此测试将验证您的控制器是否使用正确的参数成功地调用了WebSecurityHelper的登录方法
[测试]
公共无效登录\u必须\u调用\u WebSecurityLogin()
{ 
var loginModel=new loginModel(){UserName=“test”,Password=“test”}
var helperMock=new Mock();
Expect(m=>m.Login(loginModel.UserName,loginModel.Password));
var controller=新登录控制器(\u helperMock.Object);
controller.Login(loginModel,string.Empty);
验证(m=>m.Login(loginModel.UserName,loginModel.Password));
}
}
1)您可以验证当模型有效时,是否使用正确的参数调用WebSecurity.Login

2) 当WebSecurity.Login返回true时,您可以验证重定向到reutnrUrl

3) 如果模型无效,您可以验证ModelState是否包含错误和返回的ViewResult

不过,您必须重构对WebSecurity的调用,您必须模拟它。可能必须使用依赖注入技术

示例

1) 您必须将WebSecurity隐藏在接口后面,并仅通过该接口使用它(我假设您的WebSecurity静态类来自WebMatrix.WebData命名空间)

2) 在您的控制器中,您必须创建WebSecurityHelper的实例,通常是使用IoC框架(如或)完成的。但是对于这个例子,我将在controllers构造函数中初始化websecurityhelp,这仍然允许我注入mock进行测试

public class LoginController
{
   private readonly IWebSecurityHelper _helper;

   public LoginController(IWebSecurityHelper helper)
   {
     _helper = helper;
   }

   // passing your implementation of the WebSecurityHelper 
   public LoginController() : this(new WebSecurityHelper()) {}

   [HttpPost]
   [AllowAnonymous]
   [ValidateAntiForgeryToken]
   public ActionResult Login(LoginModel model, string returnUrl)
   {
       if (ModelState.IsValid && _helper.Login(model.UserName,model.PasswordpersistCookie: model.RememberMe))
       {
         return RedirectToLocal(returnUrl);
       }

       ModelState.AddModelError("", "The user name or password provided is incorrect.");

       return View(model);
   }      
}
3) 在单元测试中,必须模拟IWebSecurityHelper。我个人更喜欢很多模拟框架

[TestFixture]
公共类登录控件测试
{
//此测试将验证您的控制器是否使用正确的参数成功地调用了WebSecurityHelper的登录方法
[测试]
公共无效登录\u必须\u调用\u WebSecurityLogin()
{ 
var loginModel=new loginModel(){UserName=“test”,Password=“test”}
var helperMock=new Mock();
Expect(m=>m.Login(loginModel.UserName,loginModel.Password));
var controller=新登录控制器(\u helperMock.Object);
controller.Login(loginModel,string.Empty);
验证(m=>m.Login(loginModel.UserName,loginModel.Password));
}
}

我将在编辑后的答案中为您发布一个简单的示例。请看这篇文章:我将在编辑后的答案中为您发布一个简单的示例。请看这篇文章:
[TestFixture]
public class LoginControllerTests
{
   // this test will verify that your controller called Login method of the  WebSecurityHelper successfully with correct parameters
   [Test]
   public void LoginAction_Must_Call_WebSecurityLogin()
   { 
       var loginModel = new LoginModel() { UserName = "test", Password = "test" }

       var helperMock = new Mock<IWebSecurityHelper>();
       helperMock.Expect(m => m.Login(loginModel.UserName, loginModel.Password));
       var controller = new LoginController(_helperMock.Object);
       controller.Login(loginModel, string.Empty);
       helperMock.Verify(m => m.Login(loginModel.UserName, loginModel.Password));
   }

}