Liv*_* M. 5 .net c# unit-testing claims-based-identity asp.net-mvc-4
如何测试此代码(ASP.NET MVC 4,.NET 4.5 Web应用程序中的登录方法):
public ActionResult Login(LoginModel model, string returnUrl)
{
if (ModelState.IsValid && _userCredentialsService.ValidateUser(model.UserName, model.Password))
{
SessionAuthentication.SetAuthCookie(model.UserName, _userCredentialsService.GetUserGroups(model.UserName), model.RememberMe);
return RedirectToLocal(returnUrl);
}
}
Run Code Online (Sandbox Code Playgroud)
使用此SetAuthCookie方法:
public static void SetAuthCookie(IEnumerable<Claim> claims, bool isPersistent)
{
if (!HttpContext.Current.Request.IsSecureConnection && FormsAuthentication.RequireSSL)
{
throw new HttpException(500, "Connection is not secured with SSL");
}
var sessionToken = CreateSessionSecurityToken(CreatePrincipal(claims.ToList()), isPersistent);
FederatedAuthentication.SessionAuthenticationModule.WriteSessionTokenToCookie(sessionToken);
}
Run Code Online (Sandbox Code Playgroud)
我在访问时得到一个空引用异常SessionAuthenticationModule(在尝试获取HttpContext时提到的属性在内部抛出异常).这是堆栈跟踪:
at System.IdentityModel.Services.FederatedAuthentication.GetHttpContextModule[T]()
at System.IdentityModel.Services.FederatedAuthentication.GetHttpModule[T]()
at System.IdentityModel.Services.FederatedAuthentication.get_SessionAuthenticationModule()
Run Code Online (Sandbox Code Playgroud)
我在位于测试程序集中的app.config中制作了我的web.config的精确副本,代码在应用程序的正常运行时中运行.
我已经使用了HttpSimulator和MvcContrib测试助手,以便HttpContext得到设置,但仍然抛出异常.有没有人有如何测试它的解决方案或解决方法?
您始终可以使用 Typemock Isolator/Telerik JustMock - 它们可以模拟静态方法和 future(在代码中创建的类)。对于隔离器,一个简单的
Isolate.WhenCalled(() => FederatedAuthentication.SessionAuthenticationModule)
.WillReturn(<a fake value>);
应该能解决问题
| 归档时间: |
|
| 查看次数: |
875 次 |
| 最近记录: |