Using Rhino Mocks from VB.Net

time to read 15 min | 2978 words

I don't get a lot of chances to work on VB.Net, but when I do, it is almost always because someone has a problem with my code (or their code), and I need to look at it. The problem is that I like the syntax (sort of booish :-) ), but I keep trying to think about it like C#, so I keep getting wierd compiler errors. I am probably the only one who learns the VB syntax from opening reflector and switching to the VB.Net view.

Anyway, I was asked how you can mock event registration with Rhino Mocks in VB.Net, and here is the answer:

<TestFixture()> _

Public Class RhinoMocks_InVB

       <Test()> _

       Public Sub UsingEvents()

              Dim mocks As New MockRepository()

              Dim lv As ILoginView = mocks.CreateMock(Of ILoginView)()

 

              Using mocks.Record

                     AddHandler lv.Load, Nothing

                     LastCall.IgnoreArguments()

              End Using

 

              Using mocks.Playback

                     Dim temp As New LoginPresenter(lv)

              End Using

 

       End Sub

End Class

Where the objects we are test are:

Public Interface ILoginView

       Event Load As EventHandler

End Interface

 

Public Class LoginPresenter

       Public Sub New(ByVal view As ILoginView)

              AddHandler view.Load, AddressOf OnLoad

       End Sub

 

       Public Sub OnLoad(ByVal sender As Object, ByVal e As EventArgs)

 

       End Sub

End Class