嗨,我正在做我的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