I am a newcomer to writing automated unit tests using Rhino Mocks and
I'd appreciate any help I can get here.

I’m testing a class called ClientSearchPresenter. The
ClientSearchPresenter class has a property of type IClientSearchView,
which has two private events.

I’m stuck testing the OnViewLoaded method. It wires up two methods in
the presenter as event handlers to the View property’s events, then
calls a private method.

I want to test whether the event handlers have been successfully
attached. I have no freakin' idea how to do this, unfortunately.

Does anyone know of a way to access the property, get its events and
see whether there are any event handlers attached?

Alternatively, I could just call the events and then test whether the
code that they run has executed. I don’t know how to do this from my
test code, either, though. Does anyone have any ideas?

The following is
-       code for the class being tested
-       code for the interface that the property I want to access (the one
with the private events)
-       my test code so far.

    Public Class ClientSearchPresenter
        Inherits Presenter(Of IClientSearchView)

        Public Sub New(<Dependency()> ByVal controller As
IClientSearchController)

            _controller = controller
        End Sub

        Private _controller As IClientSearchController
        Public ReadOnly Property Controller() As
IClientSearchController
            Get
                Return _controller
            End Get
        End Property

        Public Overrides Sub OnViewLoaded()

            AddHandler View.ClearClicked, AddressOf View_ClearClicked
            AddHandler View.SearchClicked, AddressOf
View_SearchClicked

            LoadKeyOnView()
        End Sub

        Private Sub View_ClearClicked(ByVal sender As Object, ByVal e
As EventArgs)

            Controller.ClearSearch()
            LoadKeyOnView()
        End Sub

        Private Sub View_SearchClicked(ByVal sender As Object, ByVal e
As DataEventArgs(Of ClientSearchKeyDocument))

            Controller.SearchForClient(e.Data)
            LoadKeyOnView()
        End Sub

        Private Sub LoadKeyOnView()

            View.Key = Controller.SelectForSearch()
        End Sub

    End Class

    Public Interface IClientSearchView

        Event ClearClicked As EventHandler
        Event SearchClicked As EventHandler(Of DataEventArgs(Of
ClientSearchKeyDocument))

        WriteOnly Property Key() As ClientSearchKeyDocument

    End Interface

    ''<summary>
    ''A test for OnViewLoaded
    ''</summary>
    <TestMethod()> _
    Public Sub ClientSearchPresenter_OnViewLoadedTest()
        mocks = New MockRepository()
        mockState = New PrePostMockState()
        mockNavigator = New NavigatorMock(mockState)

        Dim controller As ClientSearchController
        ' Have RhinoMocks dynamically generate a controller for use in
the presenter's constructor
        controller = mocks.Stub(Of ClientSearchController)
(mockNavigator)

        ' The onViewLoaded method will access a method on the
presenter's View property, so
        ' we need to mock one up.
        Dim mockView As IClientSearchView
        mockView = mocks.Stub(Of IClientSearchView)()

        Dim fakeKey As New ClientSearchKeyDocument()
        mockView.Key = fakeKey

        Dim target As ClientSearchPresenter = New ClientSearchPresenter
(controller)
        ' target.View is instantiated as Nothing, so need to set a
view for it to access
        target.View = mockView

        target.OnViewLoaded()

        ' TODO – I want to access the events in the target.View object
and see if they
        ' have event handlers attached here  - How?
        ' TODO – alternatively, I want to raise an event that should
be handled by the
        ' target.View and see if it hits the method in the presenter –
how?

    End Sub

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Rhino.Mocks" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/RhinoMocks?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to