@Kumquat
This can be a bit frustrating to work through the first time. There
are two basic event tests I like to use:
1) test the event is registered (wired, routed, attached) correctly
2) invoke the event and test expected behavior
Here's how I might do test number 1 (in C#, sorry - I don't do VB)
[Test]
public void
ClearClickedEvent_InstantiatingTheController_RegistersEventToViewClearClicked
()
{
// ARRANGE - set up a mock of your view
var view = MockRepository.GenerateMock<IClientSearchView>();
// ACT - make a new controller
var c = new Controller(...)
// ASSERT - this is all you need to do
view.AssertWasCalled(x => x.ClearClicked += c.ViewClearClicked );
}
Test 2 is more involved and likely be more than one test, but the main
idea is to use the event raiser to raise your event, ie:
view.Raise(x => x.ClearClicked += null, this,
EventArgs.Empty); // EventArgs need not be EventArgs.Empty
and then assert a call was made that verifies the behavior you expect.
Hope that helps.
Berryl
On Jul 10, 5:38 am, ulu <[email protected]> wrote:
> Search the wiki (or Google) for GetEventRaiser
>
> ulu
>
> BTW there's no such thing as private event in an interface, you know.
>
> On Jul 7, 9:18 am, TheMightyKumquat <[email protected]> wrote:
>
> > 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
-~----------~----~----~----~------~----~------~--~---