Ciao sto facendo qualche test unitario sul mio progetto ASP.Net MVC2. Sto usando il framework Moq. Nel mio LogOnController,FormsAuthentication.SetAuthCookie mocking using Moq
[HttpPost]
public ActionResult LogOn(LogOnModel model, string returnUrl = "")
{
FormsAuthenticationService FormsService = new FormsAuthenticationService();
FormsService.SignIn(model.UserName, model.RememberMe);
}
In classe 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();
}
}
Il mio problema è come posso evitare l'esecuzione di
FormsService.SignIn(model.UserName, model.RememberMe);
questa linea. O c'è un modo per MOQ
FormsService.SignIn(model.UserName, model.RememberMe);
utilizzando Moq quadro senza cambiare il mio progetto ASP.Net MVC2.
Che cos'è SUT (System Under Test) - 'LogOnController' o' FormsAuthenticationService'? Se è il primo, è necessario fornire un falso per 'FormsAuthenticationService' e verificare che il metodo' SignIn' sia chiamato su di esso. Quest'ultimo è più difficile da testare in quanto richiede un attuale 'HttpContext' a cui aggiungere un cookie (a' HttpResponse'). –
Voglio testare LogOnController. Ho provato a simulare FormsService.SignIn (model.UserName, model.RememberMe); in questo modo, var formService = new Mock(); Ma formservice.SignIn non restituisce nulla. Come posso evitare di eseguire quella linea o come deridere quella linea. Non ho idea di come deridere quell'uso di Moq. –
Dilma