嗨,我正在做我的ASP.Net MVC2项目的单元测试。我正在使用Moq框架。在我LogOnController,FormsAuthentication.SetAuthCookie使用Moq嘲弄
[HttpPost]
public ActionResult LogOn(LogOnModel model, string returnUrl = "")
{
FormsAuthenticationService FormsService = new FormsAuthenticationService();
FormsService.SignIn(model.UserName, model.RememberMe);
}
在FormAuthenticationService类,
public class FormsAuthenticationService : IFormsAuthenticationService
{
public virtual void SignIn(string userName, bool createPersistentCookie)
{
if (String.IsNullOrEmpty(userName)) throw new ArgumentException("Value cannot be null or empty.", "userName");
FormsAuthentication.SetAuthCookie(userName, createPersistentCookie);
}
public void SignOut()
{
FormsAuthentication.SignOut();
}
}
我的问题是如何避免执行
FormsService.SignIn(model.UserName, model.RememberMe);
此行。或者是有什么办法使用起订量框架没有改变我的ASP.Net MVC2项目,起订量
FormsService.SignIn(model.UserName, model.RememberMe);
。
什么是SUT(被测系统) - LogOnController或FormsAuthenticationService?如果它是前者,则应该为'FormsAuthenticationService'提供一个伪造品,并且应该验证是否调用了SignIn'方法。后者很难单元测试,因为它需要一个当前的'HttpContext'来添加一个cookie(到'HttpResponse')。 – 2012-07-09 13:37:02
我想测试LogOnController。我试图模拟FormsService.SignIn(model.UserName,model.RememberMe); 以这种方式, var formService = new Mock(); 但formservice.SignIn不返回任何内容。我该如何避免执行该行或如何嘲笑该行。我不知道如何使用Moq来模拟。 –
Dilma
2012-07-10 04:43:43