views:

858

answers:

3

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?

A: 

It seems that this seals the deal.

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

Arnis L.
+3  A: 

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.

Steven Lyons
:( Shame really, apart from this I'm really loving Moq. If I suggest to the boss we put off development for another year so that they can get VB10 out I think he'll kill me!
Nick
A: 

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

RoyOsherove
Unfortunately as a small group of internal developers we can't justify the cost of TypeMock. Currently we are just writing methods we need to mock as functions that return a boolean and waiting for VB10.
Nick