2012-07-09 6 views
7

Witam mam zrobić test jednostki na moim projekcie ASP.Net MVC2. Używam frameworka Moq. W moim LogOnController,FormsAuthentication.SetAuthCookie szyderczy za pomocą Moq

[HttpPost] 
public ActionResult LogOn(LogOnModel model, string returnUrl = "") 
{ 
    FormsAuthenticationService FormsService = new FormsAuthenticationService(); 
    FormsService.SignIn(model.UserName, model.RememberMe); 

} 

w klasie 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(); 
     } 
    } 

Moim problemem jest to, jak można uniknąć wykonywania

FormsService.SignIn(model.UserName, model.RememberMe); 

tej linii. Czy jest jakiś sposób, aby Min

FormsService.SignIn(model.UserName, model.RememberMe); 

użyciu Moq ramy bez zmianę mojego projektu ASP.Net MVC2.

+0

Co to jest SUT (System Under Test) - "LogOnController" lub "FormsAuthenticationService"? Jeśli jest to pierwsze, należy podać fałszywe dla 'FormsAuthenticationService' i powinieneś sprawdzić, czy jest wywołana metoda' SignIn'. Ten drugi jest trudniejszy do testowania jednostkowego, ponieważ wymaga aktualnego 'HttpContext', do którego należy dodać plik cookie (do' HttpResponse'). –

+0

Chcę przetestować LogOnController. Próbowałem udawać FormsService.SignIn (model.UserName, model.RememberMe); w ten sposób, var formService = new Mock (); Ale formservice.SignIn niczego nie zwraca. Jak mogę uniknąć wykonywania tej linii lub jak kpić z tej linii. Nie mam pojęcia, jak to udowodnić za pomocą Moq. – Dilma

Odpowiedz

9

Inject IFormsAuthenticationService jako zależność do swojego LogOnController jak ten

private IFormsAuthenticationService formsAuthenticationService; 
public LogOnController() : this(new FormsAuthenticationService()) 
{ 
} 

public LogOnController(IFormsAuthenticationService formsAuthenticationService) : this(new FormsAuthenticationService()) 
{ 
    this.formsAuthenticationService = formsAuthenticationService; 
} 

Pierwszy konstruktor dla ram tak, że prawidłowe wystąpienie IFormsAuthenticationService używanego w czasie wykonywania.

Teraz w badaniach utworzyć instancję LogonController używając innego konstruktora przekazując makiety jak poniżej

var mockformsAuthenticationService = new Mock<IFormsAuthenticationService>(); 
//Setup your mock here 

Zmiana kodu działania w celu użyć pola prywatne formsAuthenticationService jak poniżej

[HttpPost] 
public ActionResult LogOn(LogOnModel model, string returnUrl = "") 
{ 
    formsAuthenticationService.SignIn(model.UserName, model.RememberMe); 
} 

Nadzieja ta pomaga. Zrobiłem dla ciebie fałszywą konfigurację. Daj mi znać, jeśli nie wiesz, jak to ustawić.

+0

Dzięki Suhas. Nie mam pojęcia, gdzie umieścić ten kod, ponieważ jestem nowy w ASP.Net u = testowanie jednostkowe. Czy miałeś na myśli, że powinienem zmienić LogOnController w projekcie mvc? Proszę, bądź uprzejmy, aby to wyjaśnić. Z góry dziękuję. – Dilma

+0

Dzięki .. Działa. Dziękuję Suhas. – Dilma

+0

Mam nadzieję, że teraz jest dla ciebie jasne. Daj mi znać, jeśli nadal masz z tym problemy. – Suhas