Question

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 is throwing error on WebSecurity.Login method.

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 is working fine.

I think, error is due to 'cookies object is not available in unit test project'

Was it helpful?

Solution

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.

FormsAuthentication.SetAuthCookie mocking using Moq

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top