如何在 ASP.NET MVC 2.0 应用程序中测试 FormsAuthentication

发布于 2024-08-21 13:13:34 字数 1625 浏览 8 评论 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

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

Ive made a interface with the following methods:

Public Interface IAuthenticationService
    Sub SetAuthentication(ByVal username As String)
    Sub Logout()
    Function IsLoggedIn() As Boolean
End Interface

My implementation looks like:

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

In my controller class, I have a action which sets the ticket on my FormsAuthentication:

    Public Function Login(ByVal username As String, ByVal password As String) As ActionResult

       _authenticationService.SetAuthentication(username)
        Return View()
    End Function

My Question is how can I test my FormsAuthentication on my authenticationservice class. Im using Xunit/Moq for writting my tests. When I Call my action I get an "System.NullReferenceException : Object reference not set to an instance of an object" which tells me that FormsAuthentication object is Null and I therefore can not set my authentication ticket.
What is the best solution to solve this. I'll be glad for some codeexamples or references to where I can get some inspirations. Specially if the solution is mocking...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嘿哥们儿 2024-08-28 13:13:34

像这样围绕 FormsAuthentication 类创建一个包装类...

Public Interface IFormsAuthentication
    Sub SignIn(ByVal userName As String, ByVal createPersistentCookie As Bool) 
    Sub SignOut() 
End Interface 


Public Class FormsAuthenticationWrapper Implements IFormsAuthentication  

    Public Sub SignIn(ByVal userName As String, ByVal createPersistentCookie As Bool) Implements IFormsAuthentication.SignIn  
        FormsAuthentication.SetAuthCookie(userName, createPersistentCookie);
    End Sub  

    Public Sub SignOut() Implements IFormsAuthentication.SignOut  
        FormsAuthentication.SignOut()  
    End Sub  

End Class 

然后您可以将 IFormsAuthentication 作为依赖项传递到您的 Authentication 类(通过构造函数)。这将允许您在编写单元测试时模拟 IFormsAuthentication 调用。 :-)

Create a wrapper class around the FormsAuthentication class like this...

Public Interface IFormsAuthentication
    Sub SignIn(ByVal userName As String, ByVal createPersistentCookie As Bool) 
    Sub SignOut() 
End Interface 


Public Class FormsAuthenticationWrapper Implements IFormsAuthentication  

    Public Sub SignIn(ByVal userName As String, ByVal createPersistentCookie As Bool) Implements IFormsAuthentication.SignIn  
        FormsAuthentication.SetAuthCookie(userName, createPersistentCookie);
    End Sub  

    Public Sub SignOut() Implements IFormsAuthentication.SignOut  
        FormsAuthentication.SignOut()  
    End Sub  

End Class 

You can then pass IFormsAuthentication in to your Authentication class as a dependancy (through the constructor). This will allow you to Mock the IFormsAuthentication call when you write your unit tests. :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文