Dim target As ClientSearchResultsPresenter = New
ClientSearchResultsPresenter(mockController)

Don't know VB.Net, but the code above, and a few other places, you are using 
the mocks before you put it in replay mode. 

Sent from my Verizon Wireless BlackBerry

-----Original Message-----
From: TheMightyKumquat <[email protected]>

Date: Tue, 21 Jul 2009 19:46:08 
To: Rhino.Mocks<[email protected]>
Subject: [RhinoMocks] Unable to set expectation on object's property -
 connected with  Generics?



I have an app written in the Model-View-Presenter pattern that I'm
testing. I am getting an exception in my test code at run-time for the
Presenter: "previous method requires a return value or an exception to
throw." This is associated with a Controller class that is a property
of the Presenter. The Controller is defined with generics. I'm both a
novice with Generics and with Rhino Mocks, and I’m stumped as to how I
can fix this error. Can anyone help, please?

The class I'm testing:
Public Class ClientSearchResultsPresenter
        Inherits Presenter(Of IClientSearchResultsView)

        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.SelectedIndexChanged, AddressOf
View_SelectedIndexChanged
            AddHandler View.SelectedPageChanged, AddressOf
View_SelectedPageChanged

            LoadSummaryOnView()
            View.SelectedPage = Controller.SelectedPage
        End Sub


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

            Controller.SetSelectedSearchResult(View.SelectedIndex)
        End Sub

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

            Controller.SelectedPage = View.SelectedPage
        End Sub

        Private Sub LoadSummaryOnView()

            View.Summary =
Controller.SummaryDocument.ClientSearchSummaryFieldsCollection
        End Sub

    End Class

The test code:
   <TestMethod()>_
    Public Sub ClientSearchResultsPresenter_OnViewLoadedTest()
        ' OnViewLoaded in the presenter does the following:
        ' - adds event handlers for the View's SelectedIndexChanged
and SelectedPageChanged events
        ' - calls the private function LoadSummaryOnView, which calls
a Controller method and assigns the result to the View.Summary
property in the presenter
        ' -  sets the View.SelectedPage property to the
Controller.SelectedPage property.

        ' Mock a controller
        Dim mockController As IClientSearchController
        mockController = mocks.StrictMock(Of IClientSearchController)
()
        Dim target As ClientSearchResultsPresenter = New
ClientSearchResultsPresenter(mockController)

        ' when the presenter calls its LoadKeyOnView method, it will
assign to the View.Summary property. Set up expectation for this write-
only property
        Dim fakeSummary As New ClientSearchSummaryDocument()
        fakeSummary.ClientSearchSummaryFieldsCollection.Add(New
ClientSearchSummaryDocument.ClientSearchSummaryFieldsDocument())

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

        ' set up expectation that during the test, the Presenter being
tested will add a
        ' handler to the View's events and that this event handler
will not be null
        AddHandler mockView.SelectedIndexChanged, Nothing
        LastCall.Constraints(Rhino.Mocks.Constraints.Is.NotNull())
        AddHandler mockView.SelectedPageChanged, Nothing
        LastCall.Constraints(Rhino.Mocks.Constraints.Is.NotNull())

        ' set up expectation that view.Summary will be written to
        mockView.Summary =
fakeSummary.ClientSearchSummaryFieldsCollection

        ' set expectation that View.SelectedPage will be called
        Rhino.Mocks.Expect.Call(mockView.SelectedPage).Return(0)
        ' set expectation that Controller.SelectedPage will be called
        Rhino.Mocks.Expect.Call(mockController.SelectedPage).Return(0)

        target.View = mockView

        ' set expectation that controller's property will be called -
ERROR OCCURS HERE
Rhino.Mocks.Expect.Call(mockController.SummaryDocument).Return
(fakeSummary)

        mocks.ReplayAll()

        target.OnViewLoaded()

        ' verify that all expectations set up for this test have been
met on all mock objects
        mocks.VerifyAll()
        Assert.IsTrue(target.View.SelectedPage =
target.Controller.SelectedPage)

    End Sub

The error message can mean that you’ve forgotten to call Replay on the
object you’re setting the expectation on.
http://codemonkey.nmonta.com/2008/09/19/previous-method-requires-a-return-value-or-an-exception-to-throw/
Not the case here – I’ve got ReplayAll in the test’s code, and
besides, I’m getting an exception on the line when I call Expect.Call,
not when the test method runs.

It can also mean that you’re defining the wrong data type for the
Expect.Call to return. 
http://lexicalclosures.blogspot.com/2008/09/cryptic-rhino-mock-exception-messages.html.
I think this is what’s happening here.

My Controller’s Interface is defined using Generics – code is this:

''' <summary>
    ''' Interface for the client registration client search
controller.
    ''' </summary>
    Public Interface IClientSearchController
        Inherits IController(Of ClientSearchKeyDocument,
InteractionDocumentBase, ClientSearchSummaryDocument)

        ' search

        ''' <summary>
        ''' Returns the current search details to display on the
client search view.
        ''' </summary>
        ''' <returns>Current search details to display on the client
search view.
        ''' </returns>
        Function SelectForSearch() As ClientSearchKeyDocument

        ''' <summary>
        ''' Clears the current search details.
        ''' </summary>
        Sub ClearSearch()

        ''' <summary>
        ''' Performs a client search with the given client details.
        ''' </summary>
        ''' <param name="key">The client details to search for.</
param>
        ''' <remarks>Navigates to the search results view on success.</
remarks>
        Sub SearchForClient(ByVal key As ClientSearchKeyDocument)

        ''' <summary>
        ''' Sets the index of the search result within the summary
document selected by
        ''' the user and navigates to the view for that result.
        ''' </summary>
        ''' <param name="index">Index of the search result within the
summary document
        ''' selected by the user.</param>
        Sub SetSelectedSearchResult(ByVal index As Integer)

        ' search results

        ''' <summary>
        ''' Gets or sets the currently selected page index for the
search results view.
        ''' </summary>
        ''' <value>The currently selected page index for the search
results view.</value>
        Property SelectedPage() As Integer

    End Interface

    Public Interface IController(Of TKey As InteractionDocumentBase,
TEntity As InteractionDocumentBase, TSummary As
InteractionDocumentBase)
        Inherits IControllerBase

        Property KeyDocument() As TKey
        Property EntityDocument() As TEntity
        Property SummaryDocument() As TSummary

    End Interface

I can use Rhino Mocks to generate a mock of this with no problems
with
mockController = mocks.StrictMock(Of IClientSearchController)()
It doesn’t seem to matter whether I choose a StrictMock or a
DynamicMock.

I notice when I step through the code in debug mode and add a Watch
for the mocked Controller, all the controller’s properties just show
as
<propertyName> - "previous method requires a return value or an
exception to throw". I can also set the expectation for the
Controller's SelectedPage property with no problems - it's setting it
for the SummaryDocument, the property hooked up via Generics, that's
causing the problem.

What’s happening here, and how can I change my test to fix it?





--~--~---------~--~----~------------~-------~--~----~
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