使用 Moq 模拟 VB.NET 方法

发布于 2024-07-28 00:11:11 字数 1567 浏览 4 评论 0原文

我正在尝试对使用成员资格提供程序更新用户详细信息的控制器操作进行单元测试。 我正在使用 Moq,到目前为止它很容易使用。

问题是我似乎无法让它模拟调用不返回任何内容的方法。

<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

模拟会员服务的设置无法编译,错误是:

重载解析失败,因为没有 可以调用可访问的“设置” 这些论点:

'公共职能 设置(TResult)(表达式为 System.Linq.Expressions.Expression(Of 系统.Func(Of 服务.会员服务, T结果)))作为 Moq.Language.Flow.ISetup(Of 服务.会员服务, TResult)': 表达式不产生 一个值。

'公共功能设置(Of TResult)(表达式为 System.Linq.Expressions.Expression(Of 系统.Func(Of 服务.会员服务, T结果)))作为 Moq.Language.Flow.ISetup(Of 服务.会员服务, TResult)':该类型的数据类型 无法推断出参数 这些论点。 指定数据 类型明确可能会纠正这个问题 错误。

'公共职能 设置(表达式为 System.Linq.Expressions.Expression(Of 系统.Action(Of 服务.IMembershipService))) 作为 Moq.Language.Flow.ISetup(Of 服务.IMembershipService)': 表达式不产生值。

我错过了什么? 每当我的类有我想要调用的方法时,我是否必须创建一个假类而不是使用 Moq?

编辑:

好的,稍微阅读一下就会发现这是由于 VB 中使用 Function() 表达 lambda 的方式造成的,而 Function() 必须有一个结果。

有没有人找到解决这个问题的方法,或者我是否必须放弃 Moq 来伪造方法?

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?

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

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

发布评论

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

评论(4

执笏见 2024-08-04 00:11:12

看来达成协议。

有点令人失望——调查花了我很多时间。 :/

It seems that this seals the deal.

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

丢了幸福的猪 2024-08-04 00:11:12

为此,Typemock 支持 VB.NET 友好的 API:
http://site.typemock.com/vbpage/ 2009/9/10/unit-testing-vbnet.html

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

逐鹿 2024-08-04 00:11:11

在 Visual Studio 2010 中使用

serviceMock.Setup(Sub(c) c.MethodName(paramName))

另外,请确保模拟实体 (serviceMock) 被模拟为接口,或者将 MethodName 声明为可重写。

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.

在你怀里撒娇 2024-08-04 00:11:11

正如您所发现的,当前版本的 VB.NET (VB9) 仅允许返回值的 lambda 方法(即 Function lambda)。 除了 创建一个函数来返回虚拟值。 我目前无法对其进行测试,因此我不确定这对于这种情况是否是可行的解决方法。

VB.NET 的下一个版本 (VB10),该语言将支持 Sub lambda,并且应该在这些情况下有所帮助。

似乎其他人也在当前的 Moq/ 方面遇到了不同程度的麻烦 VB.NET组合。

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.

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