Problem understanding the Rhino.Mock test framework

360 views
Skip to first unread message

Joseph Dineen

unread,
Apr 13, 2010, 12:40:12 PM4/13/10
to Rhino.Mocks
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

Tim Barcz

unread,
Apr 13, 2010, 3:39:40 PM4/13/10
to rhino...@googlegroups.com
It appears you are using strict mocks...which means any call not "expected" will cause the test to fail.


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




--
Tim Barcz
Microsoft C# MVP
Microsoft ASPInsider
http://timbarcz.devlicio.us
http://www.twitter.com/timbarcz

Joseph Dineen

unread,
Apr 14, 2010, 6:07:19 AM4/14/10
to Rhino.Mocks
Well I have tried it using DynamicMock instead of StrictMock and it
made no difference.
Besides, the line

> > Rhino.Mocks.Expect.Call(model.CreateNewCalendar(view.CalendarName)).Repeat.AtLeastOnce.Return(c)
Should create the expectation and I know the event is raised because I
can see the call in the event in the debugger.
The problem arises when the cal is made to the model.GetCalendar
method. There the expectation exception gets raised.
I have akso tried adding IgnoreArguments to no avial.
I am sure I am missing something but I do not know what.


On Apr 13, 8:39 pm, Tim Barcz <timba...@gmail.com> wrote:
> It appears you are using strict mocks...which means any call not "expected"
> will cause the test to fail.
>

> > rhinomocks+...@googlegroups.com<rhinomocks%2Bunsu...@googlegroups.com>

bill richards

unread,
Apr 14, 2010, 6:49:56 AM4/14/10
to Rhino.Mocks
The point that Tim is making is that, ANY call that is made on your
StrictMock, for which you HAVE NOT set an expectation for, will cause
the test to fail.

Whe you use a StrictMock, you have to set an expectation for EVERY
call made on that object. So it is completely expeted that a call to
model.GetCalendar will throw an expectation excepton, since you have
not set up such an expectation.

On Apr 14, 11:07 am, Joseph Dineen <joseph.din...@gmail.com> wrote:
> Well I have tried it using DynamicMock instead of StrictMock and it
> made no difference.
> Besides, the line
>

> > > Rhino.Mocks.Expect.Call(model.CreateNewCalendar(view.CalendarName)).Repeat.­AtLeastOnce.Return(c)

> > > 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.TestCreateNewCalendarEventIsRaisedThe­OldWay


> > > 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 rhino...@googlegroups.com.
> > > To unsubscribe from this group, send email to

> > > rhinomocks+...@googlegroups.com<rhinomocks%2Bunsubscribe@googlegrou­ps.com>


> > > .
> > > For more options, visit this group at
> > >http://groups.google.com/group/rhinomocks?hl=en.
>
> > --
> > Tim Barcz
> > Microsoft C# MVP

> > Microsoft ASPInsiderhttp://timbarcz.devlicio.ushttp://www.twitter.com/timbarcz- Hide quoted text -
>
> - Show quoted text -

Joseph Dineen

unread,
Apr 14, 2010, 7:08:26 AM4/14/10
to Rhino.Mocks
But I thought that the Line in my test

Rhino.Mocks.Expect.Call(model.CreateNewCalendar(view.CalendarName)).Repeat.AtLeastOnce.Return(c)

Sets up such an expectation?

If it does not then how does one set up the expectation?

On Apr 14, 11:49 am, bill richards <bill.richa...@greyskin.co.uk>
wrote:

> > > Microsoft ASPInsiderhttp://timbarcz.devlicio.ushttp://www.twitter.com/timbarcz-Hide quoted text -

bill richards

unread,
Apr 14, 2010, 7:42:59 AM4/14/10
to Rhino.Mocks
In that line of code you have said:

I expect the member CreateNewCalendar to be called on model using the
value view.CalendarName as a parameter.
I expect it to be called at least once and it will return c.

Nowhere have you stated that you expect model.GetCalendar to be
called.

On Apr 14, 12:08 pm, Joseph Dineen <joseph.din...@gmail.com> wrote:
> But I thought that the Line in my test
>

> Rhino.Mocks.Expect.Call(model.CreateNewCalendar(view.CalendarName)).Repeat.­AtLeastOnce.Return(c)

> > > > Microsoft ASPInsiderhttp://timbarcz.devlicio.ushttp://www.twitter.com/timbarcz-Hidequoted text -
>
> > > - Show quoted text -- Hide quoted text -

bill richards

unread,
Apr 14, 2010, 7:45:39 AM4/14/10
to Rhino.Mocks
Further to this ....

this is the reasn for my reluctance to use StrictMock : the use of a
StrictMock implies your internal knowledge of the item being mocked,
you need to know what the internal implications are for making a call.
To me, this is at far too granular a level.

On Apr 14, 12:42 pm, bill richards <bill.richa...@greyskin.co.uk>
wrote:

> ...
>
> read more »- Hide quoted text -

Joseph Dineen

unread,
Apr 14, 2010, 8:03:39 AM4/14/10
to Rhino.Mocks
Thanks,
It apprears to have been simple blindness on my part.

So can anyone point me at a good resource to recast it in the newer
AAA syntax?

On Apr 14, 12:45 pm, bill richards <bill.richa...@greyskin.co.uk>

> ...
>
> read more »

David Tchepak

unread,
Apr 14, 2010, 8:10:18 AM4/14/10
to rhino...@googlegroups.com
Not sure if it helps for this particular case, but I wrote an intro to AAA with Rhino Mocks a while back, although you'll have to translate it from C#:


Oh, you asked for a "good" resource. Never mind. ;)


--
You received this message because you are subscribed to the Google Groups "Rhino.Mocks" group.
To post to this group, send email to rhino...@googlegroups.com.
To unsubscribe from this group, send email to rhinomocks+...@googlegroups.com.

Joseph Dineen

unread,
Apr 14, 2010, 10:10:26 AM4/14/10
to Rhino.Mocks
Thanks David, but my problem is that I have been living in a .NET 2.0
world the past while and this lambda stuff is new to me.
I think I can grok what the C# code is doing but I have no idea how to
translate to the VB equavilent. I was hoping I could find examples
where someone had done it in both languages side by side.
That way I could possibly figure out the translation myself.
I also have problems setting up an expected call on a sub (void method
to C# people) but I have come across references to this on the net.
I'll have to re-read the references though because
my first stab at it is still not compiling.

Anyhoo thanks to everyone for the help and if I make no progress I
guess I'll be back.


On Apr 14, 1:10 pm, David Tchepak <tche...@gmail.com> wrote:
> Not sure if it helps for this particular case, but I wrote an intro to AAA
> with Rhino Mocks a while back, although you'll have to translate it from C#:
>

> http://www.davesquared.net/2008/10/very-basics-of-aaa-with-rhino-mock...
>
> <http://www.davesquared.net/2008/10/very-basics-of-aaa-with-rhino-mock...>Oh,


> you asked for a "good" resource. Never mind. ;)
>

> ...
>
> read more »

David Tchepak

unread,
Apr 14, 2010, 10:26:30 AM4/14/10
to rhino...@googlegroups.com
Hi Joseph,

Have you seen the VB/AAA example on the Rhino Mocks wiki?

In terms of bsaic lamda translation, I think the following C#:
  model.Stub(x => x.CreateNewCalendar(vew.CalendarName).Return(c);

Translates to something like:
  model.Stub(Function(x) x.CreateNewCalendar(view.CalendarName)).Return(c)

Best of luck with it all!
Regards,
David

> ...
>
> read more »

Reply all
Reply to author
Forward
0 new messages