拋出錯誤,因爲HttpContext的的我那叫一個控制器的HttpContext,如:測試我的控制器在.NET MVC
[Authorize(Roles = "Administrador")]
public class ApuradorController : Controller
{
private readonly Questiona2011Context _context = new Questiona2011Context();
private readonly AuthenticationService _authenticationService = new AuthenticationService();
}
的HttpContext是AuthenticationService類電話:
public class AuthenticationService
{
private IPrincipal _user = HttpContext.Current.User;
...
}
在我的項目中,在我的實例控制器發生錯誤時,我會測試控制器private IPrincipal _user = HttpContext.Current.User;
行:未將對象引用設置爲對象的實例。
我需要測試我的控制器?
我做你的建議:var authenticationService = new Mock(); var controller = new ApuradorController(authenticationService);但我收到一個異常:參數類型'Moq.Mock '不能分配給參數類型'App.Services.IAuthenticationService' –
我應該使用新的ApuradorController(authenticationService.Object); ? –
是的,你應該這樣做。另外,如果你想使用該模擬ApplicationService的User屬性,你應該設置()它。請閱讀moq documentation wiki中的Setup() - http://code.google.com/p/moq/wiki/QuickStart – archil