我正在为我们的注销方法写一个单元测试.其中包括FormsAuthentication.SignOut().但是,它会抛出一个System.NullReferenceException.
我创造了一个模拟; HttpContext(使用Moq),但它显然是丢失的东西.
我的模拟环境包含:
>一个嘲笑的HttpRequestBase请求
>一个嘲笑响应的HttpResponseBase
>在Request.Cookies上使用HttpCookieCollection,在Response.Cookies上使用另一个
>一个嘲笑用户的IPrincipal
我知道我可以去包装路线,并在它的地方注入一个空的FormsAuth包装器对象,但我真的希望避免3个额外的文件只是为了修复一行代码.那我还是好奇的答案
所以我的问题是“HttpContext允许FormsAuthentication.SignOut()执行需要什么.”
解决方法
以下是注销代码.
public static void SignOut() { Initialize(); HttpContext current = HttpContext.Current; bool flag = current.CookielessHelper.DoesCookieValueExistInOriginal('F'); current.CookielessHelper.SetCookieValue('F',null); if (!CookielessHelperClass.UseCookieless(current,false,CookieMode) || current.Request.Browser.Cookies) { string str = string.Empty; if (current.Request.Browser["supportsEmptyStringInCookieValue"] == "false") { str = "NoCookie"; } HttpCookie cookie = new HttpCookie(FormsCookieName,str); cookie.HttpOnly = true; cookie.Path = _FormsCookiePath; cookie.Expires = new DateTime(0x7cf,10,12); cookie.Secure = _RequireSSL; if (_CookieDomain != null) { cookie.Domain = _CookieDomain; } current.Response.Cookies.RemoveCookie(FormsCookieName); current.Response.Cookies.Add(cookie); } if (flag) { current.Response.Redirect(GetLoginPage(null),false); } }
看起来你需要一个CookielessHelperClass实例.太糟糕了,它是内部和密封的 – 除非你使用TypeMock,否则没有办法模拟它. 1为包装建议:)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。