【问题标题】:How to mock a method (custom behavior) with Rhino Mocks in VB.NET如何在 VB.NET 中使用 Rhino Mocks 模拟方法(自定义行为)
【发布时间】:2009-02-13 12:51:19
【问题描述】:

如何在 VB.Net 中使用 RhinoMocks 模拟一种方法?我找到的参考在 C# 中:

 Expect.Call(delegate{list.Add(0);}).IgnoreArguments() 
     .Do((Action<int>)delegate(int item) { 
     if (item < 0) throw new ArgumentOutOfRangeException(); 
 }); 

SharpDevelop 将其转换为:

Expect.Call(Function() Do
            list.Add(0)
            End Function).IgnoreArguments().Do(DirectCast(Function(item As Integer) Do
                        If item < 0 Then
                            Throw New ArgumentOutOfRangeException()
                        End If
                       End Function, Action(Of Integer)))

但这也不起作用(它无法编译)。

这就是我想要做的:创建一个新对象并调用一个设置该方法的一些属性的方法。在现实生活中,此方法将使用在数据库中找到的值填充属性。在测试中,我想使用自定义方法/委托来模拟此方法,以便我可以自己设置属性(无需进入数据库)。

在伪代码中,这是我想要做的:

 Dim _lookup As LookUp = MockRepository.GenerateMock(Of LookUp)()
 _luvalue.Expect(Function(l As LookUp) l.GetLookUpByName("test")).Do(Function(l As LookUp) l.Property = "value")

【问题讨论】:

    标签: vb.net mocking rhino-mocks


    【解决方案1】:

    不幸的是,您正在尝试同时执行 Sub lambda 和 Statement Lambda。 VS2008 都不支持(但将在即将发布的 VS 版本中)。这是适用于 VB 的扩展版本

    我在猜测 m_list 的类型

    Class MockHelper
      Dim m_list as new List(Of Object)
    
      Public Sub New() 
        Expect(AddressOf CallHelper).IgnoreArguments().Do(AddressOf Do Hepler)
      End Sub
    
      Private Sub CallHelper() 
        m_list.Add(0)
      End Sub
    
      Private Sub DoHelper(ByVal item as Integer)
        if item < 0 Then
          Throw New ArgumentOutOfRangeException
        End If
      End Sub
    End Class
    

    【讨论】:

    • @JaredPar 我正在与需要模拟(或存根)VB.NET SUB 的相同问题作斗争。你的回答似乎解决了这个问题,但我不清楚如何在 unti 测试中使用 MockHelper 类。您能提供更多详细信息吗?
    【解决方案2】:

    我从来没有模拟过带有委托和 lambda 的东西,所以我无法为这个问题提供完整的解决方案,但我确实想分享一些示例代码,用于 Rhino Mocks 3.5 中常用的“AssertWasCalled”函数vb 开发人员,因为我花了一些时间试图了解这个......(请记住,为简洁起见,以下内容保持简单)

    这是正在测试的方法 - 可能在用户对象的服务类中找到

    Public Sub DeleteUserByID(ByVal id As Integer) Implements Interfaces.IUserService.DeleteUserByID
          mRepository.DeleteUserByID(id)
    End Sub
    

    这是断言存储库方法被调用的交互式测试

      <TestMethod()> _
      Public Sub Should_Call_Into_Repository_For_DeleteProjectById()
        Dim Repository As IUserRepository = MockRepository.GenerateStub(Of IUserRepository)()
        Dim Service As IUserService = New UserService(Repository)
    
        Service.DeleteUserByID(Nothing)
    
        Repository.AssertWasCalled(Function(x) Wrap_DeleteUserByID(x))
      End Sub
    

    这是用于确保它与 vb 一起工作的 wrap 函数

      Function Wrap_DeleteUserByID(ByVal Repository As IUserRepository) As Object
        Repository.DeleteUserByID(Nothing)
    
        Return Nothing
      End Function
    

    我发现这是一个非常讨厌的解决方案,但如果它可以帮助遇到我遇到相同问题的人,那么花时间发布这个是值得的 ;)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多