用Moq模拟VB.NET方法 [英] Mocking VB.NET Methods With Moq

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

问题描述

我正在尝试对使用成员资格提供程序更新用户详细信息的控制器操作进行单元测试.我正在使用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:

'公共职能 Setup(Of TResult)(表达式为 System.Linq.Expressions.Expression(Of System.Func(的 Services.IMembershipService, TResult)))为 Moq.Language.Flow.ISetup(Of Services.IMembershipService, TResult)':表达式不产生 一个值.

'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.

'公共功能设置(的 TResult)(表达式为 System.Linq.Expressions.Expression(Of System.Func(的 Services.IMembershipService, TResult)))为 Moq.Language.Flow.ISetup(Of Services.IMembershipService, TResult)':该类型的数据类型 无法从中推断参数 这些论点.指定数据 类型明确可以纠正这个问题 错误.

'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.

'公共职能 设置(表达式为 System.Linq.Expressions.Expression(Of System.Action(的 Services.IMembershipService)))作为 Moq.Language.Flow.ISetup(Of Services.IMembershipService)': 表达式不会产生值.

'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.

我错过了什么?每当我的班级想要调用它的方法时,我是否必须创建一个伪造的班级而不是使用Moq?

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?

好吧,稍作阅读后发现这是由于lambda通过使用Function()在VB中表示的方式而产生的.

Ok, a little reading around suggests this is due to the way lambdas are expressed in VB using Function() which must have a result.

有人找到了解决这个问题的办法吗?还是我不得不放弃Moq的伪造方法?

Has anyone found a work around for this or am I going to have to ditch Moq for faking methods?

推荐答案

如您所见,当前版本的VB.NET(VB9)仅允许返回值的lambda方法(即Function lambdas).除了创建函数以返回虚拟值.我目前无法对其进行测试,因此我不确定这是否是可行的解决方法.

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.

下一个版本的VB.NET (VB10),该语言将支持Sub lambda,并且在这种情况下应该会提供帮助.

In the next version of VB.NET (VB10), the language will support Sub lambdas and should help in these cases.

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

It seems other people are also having trouble of differing degrees with the current Moq/VB.NET combination.

这篇关于用Moq模拟VB.NET方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆