Tutorial :Mocking VB.NET Methods With Moq



Question:

I am trying to unit test a controller action that uses the membership provider to update user details. I am using Moq which so far has been easy to use.

The problem is I can't seem to get it to mock calls to methods that don't return anything.

<TestMethod()> _  Public Sub Can_Update_User()    ' Arrange    _membershipService.Setup(Function(x) x.UpdateUser(It.IsAny(Of MembershipUser)))    Dim controller As New UsersController(_membershipService.Object, _roleProvider.Object, _supportWorksService.Object, _portalClientService.Object)    ' Act    Dim result As ViewResult = controller.Edit("testUser", New FormCollection)    ' Assert    Assert.AreEqual("Index", result.ViewName)  End Sub  

The setup of the mocked membership service won't compile, the error is:

Overload resolution failed because no accessible 'Setup' can be called with these arguments:

'Public Function Setup(Of TResult)(expression As System.Linq.Expressions.Expression(Of System.Func(Of Services.IMembershipService, TResult))) As Moq.Language.Flow.ISetup(Of Services.IMembershipService, TResult)': Expression does not produce a value.

'Public Function Setup(Of TResult)(expression As System.Linq.Expressions.Expression(Of System.Func(Of Services.IMembershipService, TResult))) As Moq.Language.Flow.ISetup(Of Services.IMembershipService, TResult)': Data type(s) of the type parameter(s) cannot be inferred from these arguments. Specifying the data type(s) explicitly might correct this error.

'Public Function Setup(expression As System.Linq.Expressions.Expression(Of System.Action(Of Services.IMembershipService))) As Moq.Language.Flow.ISetup(Of Services.IMembershipService)': Expression does not produce a value.

What have I missed? Am I going to have to create a fake class rather than use Moq any time my class has a method I want to call on it?

Edit:

Ok, a little reading around suggests this is due to the way lambdas are expressed in VB using Function() which must have a result.

Has anyone found a work around for this or am I going to have to ditch Moq for faking methods?


Solution:1

As you've found, the current version of VB.NET (VB9) only allows lambda methods that return a value (ie. Function lambdas). There's not really much you can do about that other than to create a function to return a dummy value. I can't test it at the moment, so I'm not sure that is a viable workaround for this case.

In the next version of VB.NET (VB10), the language will support Sub lambdas and should help in these cases.

It seems other people are also having trouble of differing degrees with the current Moq/VB.NET combination.


Solution:2

In Visual Studio 2010 use

serviceMock.Setup(Sub(c) c.MethodName(paramName))  

Also, make sure that the mocked entity (serviceMock) is either mocked as an interface, or has MethodName declared as overrideable.


Solution:3

It seems that this seals the deal.

Kind a dissapointing - investigation took me quite a lot of time. :/


Solution:4

Typemock has support for VB.NET friendly API for that purpose: http://site.typemock.com/vbpage/2009/9/10/unit-testing-vbnet.html


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »