开发者

How to test FormsAuthentication in a asp.net mvc 2.0 app with

开发者 https://www.devze.com 2022-12-20 09:43 出处:网络
Ive made a interface with the following methods: Public Interface IAuthenticationService Sub SetAuthentication(ByVal username As String)

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

    Publi开发者_运维问答c 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...


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. :-)

0

精彩评论

暂无评论...
验证码 换一张
取 消