NET中使用Moq的VerifySet

NET中使用Moq的VerifySet

本文介绍了在VB.NET中使用Moq的VerifySet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个功能可以更新asp.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

我正在尝试测试此功能,以确保正确设置IsApproved属性

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

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

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

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

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

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

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

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

Ok, turns out it's not quite so straight forward in VB.

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

需要成为

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

进一步

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

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)

对于

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)

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

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

推荐答案

您要使用以下内容(来自 http://code.google.com/p/moq/wiki/QuickStart ):

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

现在,您要输入的内容是一个比较而不是一个赋值,因此即使Moq毫无例外地处理了该语句,也仍然不会按照您的意思行事.

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.

这篇关于在VB.NET中使用Moq的VerifySet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 04:23