我用以下方法制作了一个接口:
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
我的问题是如何在我的 authenticationservice 类上测试我的 FormsAuthentication。我使用 Xunit/Moq 来编写我的测试。当我调用我的操作时,我得到一个“System.NullReferenceException:对象引用未设置为对象的实例”,它告诉我 FormsAuthentication 对象为 Null,因此我无法设置我的身份验证票。解决此问题的最佳解决方案是什么。我会很高兴有一些代码示例或参考资料,我可以从中获得一些灵感。特别是如果解决方案是嘲笑......