开发者

Simple Moq ? - Problem with .Verify

just a simple question here. I've used Moq for awhile but, as of yet, have only used it for stubbing and not for mocking. I am trying to introduce our developers to unit testing. I set up a simple example to explain the concepts but i can't seem to get it working. Probably something simple so i thought i would just ask you all to see what i'm doing wrong:

    <Test()> _
Public Sub Divide_DivideByZero_LogsError()
    Dim mock = New Mock(Of ILogger)
    With mock
        Dim calc = New MyCalculator(New CalculatorData, .Object)
        calc.Divide(55, 0)
        .Verify(Function(x) CType(x,ILogger).WriteError(it.IsAny(of String),It.IsAny(Of String))))
    End With
End Sub

I'm using Moq version 3.2.416.3. I get an error on the .verify 开发者_StackOverflow中文版telling me that i'm calling it with incorrect arguments. I'm just trying to verify that .WriteError was called. any help would be appreciated.

Edit: Ok everyone, if i change ".WriteError" from a sub (void return) to a function that returns a boolean it works. WriteError doesn't really need to be a function. Does anyone know why a sub wouldn't work?


Edit: Ok everyone, if i change ".WriteError" from a sub (void return) to a function that returns a boolean it works. WriteError doesn't really need to be a function. Does anyone know why a sub wouldn't work?

As far, as I remember, VB9 does not support anonymous Subs (only Functions) and it's a serious show-stopper for Moq usage in VB.net. So, as soon, as you have changed WriteError signature from Sub to Function, compiler have successfully resolved return type for anonymous function in Verify parametrer.


I think you need to make it Verifiable() before actually calling Verify(). I'm not sure if it can do automatic verifiability though.

And IMHO I find using VerifyAll() much easier than verifying individual methods. i.e.:

mock.Setup(Function(x) CType(x, ILogger).WriteError(It.IsAny(Of String), It.IsAny(Of String))) _
    .Verifiable()
mock.Object.WriteError("test", "Test")
mock.VerifyAll()

Not sure if I get all the method names/signature right though but you should get the idea.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜