我有一个Azure的MVC4项目。我想写简单的测试来检查用户登录。我的登录操作定义为
[HttpPost]
public ActionResult LogOn(LogOnModel model, string returnUrl)
{
if (ModelState.IsValid)
{
if (Membership.ValidateUser(model.UserName, model.Password))
{
FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
&& !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
{
return Redirect(returnUrl);
}
else
{
return RedirectToAction("Index", "Home");
}
}
else
{
ModelState.AddModelError("", "The user name or password provided is incorrect.");
}
}
// If we got this far, something failed, redisplay form
return View(model);
}
和我的测试方法
[TestMethod]
public void UserValidLoginTest()
{
// Arrange
AccountController controller = new AccountController();
// Act
LogOnModel model = new LogOnModel()
{
UserName = "user@example.com",
Password = "pass1234"
};
var result = controller.LogOn(model, null) as RedirectToRouteResult;
// Assert
Assert.IsNotNull(result);
Assert.AreEqual(result.RouteValues["action"], "Index");
}
我有两个问题,首先是那个
FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
返回NullReferenceException以及
Url.IsLocalUrl(returnUrl)
做同样的事情。我该怎么解决这个问题?我怎么测试呢?我也可以用Selenium或Moq编写集成测试,但我不确定如何用Azure测试它。任何想法如何处理这个问题?