Hi, I am new to Rhino Mocks and I have tried to post this message
before but it does not seem to have appeared on the messageboard nor
did I get any indication that the moderators have seen the message.
While i have been using Unit Testing and TDD for a while I have not
gotten into mocking in any major way. I did use DotNetMock for a while
in a single project and I want to refactor parts of it using VB.NET
2010 as a learning excersise but found that DotNetMock does not play
well with MSUnitTest and appears to be a dead project.
So I decided to learn Rhino Mocks after some research on the net.
To do this I decided to implement a view model comtoller application
and communicate from the view to the controller with events, call
stuff from the model in the event handlers on the controller and see
how it works out. I also decided to use the older Replay verify syntax
since I have mostly worked with 2005 and earlier and have not yet
looked at lambda functions. Also the only examples in vb I could find
used this systax and while I could see that the C# lambda stuff was
trying to do I have no idea of how to implement that code in vb.
So some code ( the application aim to generate fantasy calendars for
rpgs.)
''' <summary>
''' This the view of the calendars
''' </summary>
Public Interface ICalendarsView
'the events represent user actions that are addressed by the model
Event CreateNewCalendar As EventHandler
Event Save As EventHandler
Event GetCalendarList As EventHandler
Event GetCalendar As EventHandler
Property CalendarName As String
......
'these methods allow the controller to pass data to the view
Sub ShowPreviewForYear(ByVal yearNumber As Integer)
Sub SetCalendar(ByVal c As Calendar)
Sub SetCalendarList(ByVal calList As List(Of Calendar))
End Interface
''' <summary>
''' the Model
''' </summary>
''' <remarks></remarks>
Public Interface ICalendarEngine
Function GetCalendars() As List(Of Calendar)
Function GetCalendar(ByVal name As String) As Calendar
Function CreateNewCalendar(ByVal name As String) As Calendar
Sub SaveCalendar(ByVal c As Calendar)
End Interface
The controller
'' <summary>
''' Controller of the view
''' </summary>
''' <remarks>Instantiated and refers to the model and the view</
remarks>
Public Class CalendarsController
Private WithEvents _view As ICalendarsView
Private _model As ICalendarEngine
Public Sub New(ByVal view As ICalendarsView, ByVal model As
ICalendarEngine)
If IsNothing(view) Or IsNothing(model) Then
Throw New ArgumentNullException("Passed parameters were
not instantiated")
End If
_view = view
_model = model
'Add the event handlers
AddHandler _view.CreateNewCalendar, AddressOf
OnCreateNewCalendar
End Sub
Private Sub OnCreateNewCalendar(ByVal sender As Object, ByVal e As
EventArgs)
Dim c As Calendar = _model.GetCalendar(CType(sender,
ICalendarsView).CalendarName)
End Sub
The test
Imports System.Text
Imports ArdoughterSoftware.FantasyCalendar
Imports Rhino.Mocks
Imports Rhino.Mocks.Interfaces
<TestClass()>
Public Class TestCalendarsController
Private testContextInstance As TestContext
'''<summary>
'''Gets or sets the test context which provides
'''information about and functionality for the current test run.
'''</summary>
Public Property TestContext() As TestContext
Get
Return testContextInstance
End Get
Set(ByVal value As TestContext)
testContextInstance = value
End Set
End Property
Private moMockery As MockRepository
' Use TestInitialize to run code before running each test
<TestInitialize()> Public Sub MyTestInitialize()
moMockery = New MockRepository
End Sub
'
' Use TestCleanup to run code after each test has run
<TestCleanup()> Public Sub MyTestCleanup()
moMockery.VerifyAll()
End Sub
Public Sub TestCreateNewCalendarEventIsRaisedTheOldWay()
Dim e As New EventArgs
Dim c As New Calendar
Dim view As ICalendarsView
view = moMockery.Stub(Of ICalendarsView)()
Dim model As ICalendarEngine
model = moMockery.StrictMock(Of ICalendarEngine)()
Dim calendarName As String = "Dale Reckoning"
view.CalendarName = calendarName
AddHandler view.CreateNewCalendar, Nothing
LastCall.Constraints(Constraints.Is.NotNull)
Dim CreateNewCalendarEventRaiser As IEventRaiser =
LastCall.GetEventRaiser()
Rhino.Mocks.Expect.Call(model.CreateNewCalendar(view.CalendarName)).Repeat.AtLeastOnce.Return(c)
moMockery.ReplayAll()
Dim controller = New CalendarsController(view, model)
CreateNewCalendarEventRaiser.Raise(CType(view, Object), e)
End Sub
End Class
The Error message
Test method
TestCalendars.TestCalendarsController.TestCreateNewCalendarEventIsRaisedTheOldWay
threw exception:
Rhino.Mocks.Exceptions.ExpectationViolationException:
ICalendarEngine.GetCalendar("Dale Reckoning"); Expected #0, Actual #1.
TestCleanup method TestCalendars.TestCalendarsController.MyTestCleanup
threw exception. Rhino.Mocks.Exceptions.ExpectationViolationException:
Rhino.Mocks.Exceptions.ExpectationViolationException:
ICalendarEngine.GetCalendar("Dale Reckoning"); Expected #0, Actual
#1..
Now Googling that message, would indicate that the method and the
event raise are not getting the parameters they expect and that they
are expecting a particular instance of an object. Which I do not
understand as they are getting the same instance of the object. I have
tried placing IgnoreArguments qualifiers on the expected calls and the
GetEventRiser method but it does not appear to make any difference.
What am I not getting? I am sure it is one of those things that is
obvious once you know it,
Also if I do get this code working how would i put it in AAA syntax?
Thanks
Joe
--
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.