开发者

Using Moq's VerifySet in VB.NET

I have a function that updates a user in the asp.net membership provider.

<AcceptVerbs(HttpVerbs.Post)>
Public Function EnableUser(ByVal id As String) As JsonResult
  Dim usr As StargatePortalUser = _membershipService.GetUser(id, Nothing)
  usr.IsApproved = True
  _membershipService.UpdateUser(usr)
  Dim response As New AjaxResponse(usr.UserName)
  Return Json(response)
End Function

I am trying to test this function to ensure the IsApproved property is set correctly

<TestMethod()>
Public Sub Service_Can_Enable_A_User_Account()
  ' Arrange
  Dim usr As New Mock(Of MembershipUser)
  usr.SetupProperty(Function(u) u.IsApproved)

  _membershipService.Setup(Function(m) m.GetUser(It.IsAny(Of String), It.IsAny(Of Boolean))).Returns(usr.Object)

  Dim target As New UsersController(_membershipService.Object)
  target.ControllerContext = New ControllerContext(FakeAuthenticatedHttpContext("testuser", String.Empty, True, True, False), New RouteData, target)

  ' Act
  Dim actual As JsonResult = target.EnableUser("userId")

  ' Assert
  Assert.IsTrue(DirectCast(actual.Data, AjaxResponse).Success)
  _membershipService.Verify(Sub(m) m.UpdateUser(It.IsAny(Of MembershipUser)), Times.Once)
  usr.Verify(Function(u) u.IsApproved = True)
End Sub

When I try to verify that the IsApproved property has been set to True an exception is returned:

System.ArgumentException: Expression is not a method invocation: u => (u.IsApproved == True)

There are so few examples of using Moq in VB that I can't figure this out, any help would be appreciated.

This is an ASP.NET MVC2 app in VB.NET 10 (.NET 4.0)

EDIT:

Ok, turns out it's not quite so straight f开发者_开发百科orward in VB.

usr.Verify(Function(u) u.IsApproved = True)

needs to be

usr.VerifySet(Function(u) InlineAssignHelper(u.IsApproved, True))

and you need to add the following function:

Private Shared Function InlineAssignHelper(Of T)(ByRef target As T, ByVal value As T) As T
  target = value
  Return value
End Function

FURTHER EDIT:

Thinking around the problem I arrived at a more simple solution. I changed

Dim usr As New Mock(Of MembershipUser)
usr.SetupProperty(Function(u) u.IsApproved)
_membershipService.Setup(Function(m) m.GetUser(It.IsAny(Of String), It.IsAny(Of Boolean))).Returns(usr.Object)

for

Dim usr As New Mock(Of MembershipUser)
usr.SetupProperty(Function(u) u.IsApproved)
Dim usrObj = usr.Object
_membershipService.Setup(Function(m) m.GetUser(It.IsAny(Of String), It.IsAny(Of Boolean))).Returns(usrObj)

and then can replace

usr.VerifySet(Function(u) InlineAssignHelper(u.IsApproved, True))

with the more straightforward

Assert.IsTrue(usrOb.IsApproved)

Sometimes I just don't see the simple solution :)


You want to use the following (from http://code.google.com/p/moq/wiki/QuickStart):

// or verify the setter directly mock.VerifySet(foo => foo.Name = "foo");

Right now the thing that you're feeding in is a comparison rather than an assignment, so even if Moq did process the statement without an exception, it would still not be Doing What You Mean.


Well this may be a little late, but I was facing the same problem and the solution was way simpler then using a InlineAssignHelper method.

Just change the Function to a Sub and it should work.

So try this instead:

usr.VerifySet(Sub(u) u.IsApproved = True)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜