Mocking VB.NET Methods With Moq

孤人 提交于 2019-12-06 00:15:54

问题


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?


回答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.




回答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.




回答3:


It seems that this seals the deal.

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




回答4:


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



来源:https://stackoverflow.com/questions/1157679/mocking-vb-net-methods-with-moq

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!