tags:

views:

77

answers:

2

Hi, 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 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?

A: 

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.

chakrit
Actually the way i was doing it will work if i change "WriteError" from a sub to a function that returns a boolean. I will update my post. I really doing know why it wouldn't work
+1  A: 

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.

Valera Kolupaev
Valera, thanks that was the answer. It just sucks that everything we want to mock has to be a function. I really like Moq but i guess its nothing they can do since its the languages fault and you have to use Moq through a fluent interface.