UnitTest for WebSecurity中出错.登录简单成员身份



我正在为登录过程编写一个单元测试,并使用了简单的成员api。从web项目来看,登录运行良好,但在单元测试中。简单成员api引发异常。

API在WebSecurity.Login方法上引发错误。

System.NullReferenceException被捕获HResult=-2147467261Message=对象引用未设置为对象的实例。Source=System.WebStackTrace:位于System.Web.Security.FormsAuthentication.SetAuthCookie(字符串用户名,布尔值createPersistentCookie,字符串strCookiePath)位于System.Web.Security.FormsAuthentication.SetAuthCookie(字符串用户名,布尔值createPersistentCookie)位于WebMatrix。WebData。WebSecurity.Login(字符串用户名,字符串密码,布尔持久Cookie)在[代码]内部异常:

WebSecurity.UserExists运行正常。

我认为,错误是由于"cookie对象在单元测试项目中不可用">

真正的WebSecurity.Login在后台使用FormsAuthentication.SetAuthCookie,由于FormsAuthentication.SetAuthCookie使用旧的httpcontext api,因此即使使用moq httpcontext,它也会引发异常。

http://forums.asp.net/t/1871234.aspx

所以解决方案是依赖注入。

FormsAuthentication.SetAuthCookie使用Moq模拟

http://forums.asp.net/post/5257516.aspx

最新更新