3

I have a problem with VB9 and Moq.

I need to call a verify on a Sub. Like so:

logger.Verify(Function(x) x.Log, Times.AtLeastOnce)

And my logger looks like this:

Public Interface ILogger
    Sub Log()
End Interface

But with VB this is not possible, because the Log method is a Sub, and thereby does not produce a value.

I don't want to change the method to be a function.

Whats the cleanest way of working around this limitation and is there any way to wrap the Sub as a Function like the below?

logger.Verify(Function(x) ToFunc(AddressOf x.Log), Times.AtLeastOnce)

I have tried this, but i get:

Lambda Parameter not in scope

Luhmann
  • 3,860
  • 26
  • 33

2 Answers2

1

VB10 allows for the usage of Lambada Subs.

Have you tried a simple wrapper, such as:

Public Function Wrapper(source as Action) as Boolean  
    source.Invoke()   
    Return True 
End Function
M.A. Hanin
  • 8,044
  • 33
  • 51
-1

In 2010 if its a Sub and not a Function just replace Function with Sub.

logger.Verify(Sub(x) x.Log, Times.AtLeastOnce)

Ian Hern
  • 641
  • 1
  • 8
  • 16
  • i know this technically doesn't answer the question but I was using 2010 and stumbled upon this, so after i found the answer i came back to post it – Ian Hern Mar 17 '11 at 20:31