我正在尝试在单元测试中测试身份验证:
// code in Unit Test method
var loginModel = new LoginModel();
loginModel.UserName = "userhere";
loginModel.Password = "passhere";
loginModel.RememberMe = false;
var accountController = CreateAccountController();
accountController.Login(loginModel, "");
protected static AccountController CreateAccountController()
{
var routes = new RouteCollection();
var controller = new AccountController();
controller.ControllerContext = new ControllerContext(HttpContextBaseMock.Object, new RouteData(), controller);
controller.Url = new UrlHelper(new RequestContext(HttpContextBaseMock.Object, new RouteData()), routes);
return controller;
}
但是当调用WebSecurity.Login方法时,我得到一个NullReferenceException。调用SetAuthCookie时,似乎错误是成员资格提供者失败。
知道如何解决这个问题吗?
我应该尝试模仿这种方法吗?