在 VB.NET 中使用 Moq 的VerifySet

发布于 2024-09-09 06:27:20 字数 2599 浏览 1 评论 0原文

我有一个功能可以更新 ASP.NET 会员资格提供程序中的用户。

<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

我正在尝试测试此函数以确保 IsApproved 属性设置正确

<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

当我尝试验证 IsApproved 属性是否已设置为 True 时,返回异常:

System.ArgumentException:表达式不是方法调用:u => (u.IsApproved == True)

在 VB 中使用 Moq 的例子很少,我无法弄清楚,任何帮助将不胜感激。

这是 VB.NET 10 (.NET 4.0) 中的 ASP.NET MVC2 应用程序

编辑:

好吧,事实证明它在 VB 中并不是那么简单。

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

需要

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

,您需要添加以下功能:

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

进一步编辑:

思考这个问题,我得出了一个更简单的解决方案。我改变了

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 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)

可以

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

用更简单的

Assert.IsTrue(usrOb.IsApproved)

替换有时我只是看不到简单的解决方案:)

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 forward 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 :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烏雲後面有陽光 2024-09-16 06:27:20

您想要使用以下内容(来自 http://code.google.com/p/起订量/wiki/快速入门):

//或者直接验证setter
mock.VerifySet(foo => foo.Name = "foo");

现在您输入的是比较而不是赋值,因此即使 Moq 确实毫无例外地处理了该语句,它仍然不是“Doing What You Mean”。

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.

鹿港小镇 2024-09-16 06:27:20

好吧,这可能有点晚了,但我面临着同样的问题,并且解决方案比使用 InlineAssignHelper 方法更简单。

只需将函数更改为,它就应该可以工作。

所以试试这个:

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

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文