VB.NET Mocking Method with Moq

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

The problem is that I can't get him to make fun of calls to methods that return nothing.

<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 

Configuring a wearing membership service will not compile, error:

Overload error, because there is no accessible "Setup" you can call using these arguments:

'Public function Customization (from TResult) (expression As System.Linq.Expressions.Expression (From System.Func (From Services.IMembershipService, TResult))) As Moq.Language.Flow.ISetup (From Services.IMembershipService, TResult)' : expression does not produce value.

'Setting public functions (from TResult) (expression As System.Linq.Expressions.Expression (From System.Func (From Services.IMembershipService, TResult))) As Moq.Language.Flow.ISetup (From Services.IMembershipService, TResult)' : data type (s) of type parameter cannot be inferred from these arguments. Specifying a data type can explicitly fix this error.

'Public function Customization (expression As System.Linq.Expressions.Expression (From System.Action (From Services.IMembershipService))) As Moq.Language.Flow.ISetup (From Services.IMembershipService)': The expression does not give a value.

What did I miss? Should I create a fake class and not use Moq anytime my class has a method that I want to call?

Edit:

Well, a little reading around suggests that this is due to how lambdas are expressed in VB using Function (), which should have a result.

Has anyone found a job for this, or will I have to cut Moq for fake methods?

+6
unit-testing moq
source share
4 answers

As you already found, the current version of VB.NET (VB9) allows only lambda methods to return a value (i.e. Function lambdas). Not much can be done except to create a function to return a dummy value . I cannot verify this at the moment, so I'm not sure if this is a viable solution 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 that other people also have problems of varying degrees with the current combination of Moq / VB.NET.

+6
source share

In Visual Studio 2010 use

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

Also, make sure that the mocked object ( serviceMock ) is either mocked as an interface, or has the name MethodName as overridden.

+7
source share

This one seems to be closing a deal.

Good disappointment - the investigation took me quite a while.: /

+1
source share

For this purpose, Typemock supports a VB.NET-friendly API: http://site.typemock.com/vbpage/2009/9/10/unit-testing-vbnet.html

0
source share

All Articles