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