title: Mocking VB.NET Methods With Moq
body: <p>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.</p>
  
  <p>The problem is I can't seem to get it to mock calls to methods that don't return anything.</p>
  
  <pre><code>&lt;TestMethod()&gt; _
  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
  </code></pre>
  
  <p>The setup of the mocked membership service won't compile, the error is:</p>
  
  <blockquote>
    <p>Overload resolution failed because no
    accessible 'Setup' can be called with
    these arguments:   </p>
    
    <p>'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.   </p>
    
    <p>'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. </p>
    
    <p>'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.</p>
  </blockquote>
  
  <p>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?</p>
  
  <p>Edit:</p>
  
  <p>Ok, a little reading around suggests this is due to the way lambdas are expressed in VB using Function() which must have a result.</p>
  
  <p>Has anyone found a work around for this or am I going to have to ditch Moq for faking methods?</p>