抛出错误,因为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