我尝试实现身份验证模拟,我想使用Moq。但()
显示Expression expected.
AuthenticationManager.Test.vb
<TestMethod()> Public Sub Login()
' arrange
Dim _controller As AuthenticationManager = New AuthenticationManager
Dim httpContextBase As HttpContextBase = Mock < httpContextBase > ()
' Identiy
Dim result As Boolean = _controller.Login(HttpContextBase, Identiy)
...
AuthenticationManager.vb
Public Shared Sub Login(httpContextBase As HttpContextBase, identity As ClaimsIdentity)
Dim ctx = httpContextBase.Request.GetOwinContext()
Dim authenticationManager = ctx.Authentication
authenticationManager.SignIn(identity) ' Let's go Cookie!
End Sub
你能帮帮我吗?
答案 0 :(得分:0)
你需要实例化Mock,然后从中获取你想要的对象
<TestMethod()> Public Sub Login()
' arrange
Dim _controller As AuthenticationManager = New AuthenticationManager
Dim httpContextMock As Mock<HttpContextBase> = New Mock<HttpContextBase>()
Dim httpContextBase As HttpContextBase = httpContextMock.Object
...
您尝试将变量声明为HttpContextBase
并将其初始化为Mock < HttpContextBase >
,并在原始帖子中遗漏了New
关键字
最后Don't mock HttpContext。他不喜欢被嘲笑!