如何在 ASP.NET MVC 2.0 应用程序中测试 FormsAuthentication
我使用以下方法创建了一个接口:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
像这样围绕 FormsAuthentication 类创建一个包装类...
然后您可以将 IFormsAuthentication 作为依赖项传递到您的 Authentication 类(通过构造函数)。这将允许您在编写单元测试时模拟 IFormsAuthentication 调用。 :-)
Create a wrapper class around the FormsAuthentication class like this...
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. :-)