错误的UnitTest为WebSecurity.Login简单会员 [英] Error in UnitTest for WebSecurity.Login simple membership
问题描述
我写登录过程单元测试,并已使用了简单的成员身份API。
从Web项目,登录工作正常,但在单元测试。简单的会员API是提高例外。
I am writing a unit test for login process and have used simple membership api. from web project, login is working fine, but while in unit test. simple membership api is raising exception.
API是扔在WebSecurity.Login方法错误。
API is throwing error on WebSecurity.Login method.
System.NullReferenceException被抓
的HResult = -2147467261
消息=对象引用未设置到对象的实例。
来源=的System.Web
堆栈跟踪:
在System.Web.Security.FormsAuthentication.SetAuthCookie(用户名字符串,布尔createPersistentCookie,字符串strCookiePath)
在System.Web.Security.FormsAuthentication.SetAuthCookie(用户名字符串,布尔createPersistentCookie)
在WebMatrix.WebData.WebSecurity.Login(用户名字符串,字符串密码,布尔persistCookie)
在[code]
的InnerException:
System.NullReferenceException was caught HResult=-2147467261 Message=Object reference not set to an instance of an object. Source=System.Web StackTrace: at System.Web.Security.FormsAuthentication.SetAuthCookie(String userName, Boolean createPersistentCookie, String strCookiePath) at System.Web.Security.FormsAuthentication.SetAuthCookie(String userName, Boolean createPersistentCookie) at WebMatrix.WebData.WebSecurity.Login(String userName, String password, Boolean persistCookie) at [ code ] InnerException:
WebSecurity.UserExists工作正常。
WebSecurity.UserExists is working fine.
我认为,错误是由于'饼干对象未在单元测试项目中可用
I think, error is due to 'cookies object is not available in unit test project'
推荐答案
真的遇到<一个href=\"http://msdn.microsoft.com/en-us/library/webmatrix.webdata.websecurity.login%28v=vs.111%29.aspx\"相对=nofollow> WebSecurity.Login 用途的幕后FormsAuthentication.SetAuthCookie ,
因为FormsAuthentication.SetAuthCookie使用旧的HttpContext API,因此即使有<一个href=\"http://o2platform.word$p$pss.com/2011/04/05/mocking-httpcontext-htt$p$pquest-and-htt$p$psponse-for-unittests-using-moq/\"相对=nofollow>起订量的HttpContext 它抛出异常。
acually WebSecurity.Login uses FormsAuthentication.SetAuthCookie behind the scene, since FormsAuthentication.SetAuthCookie uses old httpcontext api therefore even with moq httpcontext it is throwing exception.
http://forums.asp.net/t/1871234.aspx
这样的解决方案是依赖注入。
so solution is to dependency injection.
<一个href=\"http://stackoverflow.com/questions/11395813/formsauthentication-setauthcookie-mocking-using-moq\">FormsAuthentication.SetAuthCookie使用嘲讽起订量
http://forums.asp.net/post/5257516.aspx
这篇关于错误的UnitTest为WebSecurity.Login简单会员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!