How to mock a method (custom behavior) with Rhino Mocks in VB.NET

你离开我真会死。 提交于 2019-12-06 14:01:45

Unfortunately you're attempting to do both a Sub lambda and a Statement Lambda. Neither are supported in VS2008 (but will be in the upcoming version of VS). Here is the expanded version that will work for VB

I'm guessing at the type of 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

I have never mocked something w/ both a delegate and a lambda so I can't give a full solution to this problem, but I did want to share some example code for the usual "AssertWasCalled" function in Rhino Mocks 3.5 for vb developers because I spent some time trying to grok this... (keep in mind the below is kept simple for brevity)

This is the method under test - might be found inside a service class for the user object

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

This is the interactive test to assert the repository method gets called

  <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

This is the wrap function used to ensure this works w/ vb

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

    Return Nothing
  End Function

I found this to be a very nasty solution, but if it helps someone w/ the same issues I had it was worth the time it took to post this ;)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!