在VB中使用Moq进行单元测试中的表达式

时间:2016-04-27 09:55:32

标签: vb.net mocking moq

我尝试实现身份验证模拟,我想使​​用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

你能帮帮我吗?

1 个答案:

答案 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。他不喜欢被嘲笑!