views:

358

answers:

2

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

A: 

Odd... Aren't you "Arranging" your tests before "Acting?" Shouldn't you instantiate your controller, injecting into it the mock of your IAuthenticationService prior to running your tests?

Will
+1  A: 

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

Russell Giddings