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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With