1
伊夫测试FormsAuthentication在asp.net的MVC 2.0应用进行的有以下方法接口:如何使用
Public Interface IAuthenticationService
Sub SetAuthentication(ByVal username As String)
Sub Logout()
Function IsLoggedIn() As Boolean
End Interface
我的实现是这样的:
Public Class Authentication
Implements IAuthenticationService
Public Sub Logout() Implements IAuthenticationService.Logout
FormsAuthentication.SignOut()
LoggedIn = False
End Sub
Public Sub SetAuthentication(ByVal username As String) Implements IAuthenticationService.SetAuthentication
FormsAuthentication.SetAuthCookie(username, True)
LoggedIn = True
End Sub
Public Function IsLoggedIn() As Boolean Implements IAuthenticationService.IsLoggedIn
If LoggedIn Then Return True
Return False
End Function
Private _isLoggedIn As Boolean = false
Public Property LoggedIn() As Boolean
Get
Return _isLoggedIn
End Get
Set(ByVal value As Boolean)
_isLoggedIn = value
End Set
End Property
End Class
在我的控制器类,我拥有这台对我的FormsAuthentication票证一个动作:
Public Function Login(ByVal username As String, ByVal password As String) As ActionResult
_authenticationService.SetAuthentication(username)
Return View()
End Function
我的问题是如何测试我的FormsA认证服务类上的认证。我使用Xunit/Moq写我的测试。当我调用我的操作时,我得到一个“System.NullReferenceException:对象引用未设置为对象的实例”,它告诉我FormsAuthentication对象为Null,因此我无法设置身份验证票证。 什么是解决这个问题的最佳解决方案。我会很高兴看到一些代码示例或参考资料,以便我可以获得一些启示。特别是如果该解决方案是嘲讽......
我推荐这个答案。 – Neeta 2013-02-20 11:12:20