Problem with stub and outref

32 views
Skip to first unread message

devghost

unread,
Nov 16, 2009, 2:55:42 AM11/16/09
to Rhino.Mocks

Hi,

I'm fairly new to mocking with Rhino Mocks so bare with me :o)

I got a test where I have stubbed my domain and want a method's
ByRef parameter to return a specific value, but no matter what I try
it never returns anything. This is what the test looks like.


<TestFixture()> _
Public Class When_typing_a_search_string_shorter_than_two_characters2
Inherits ContextSpecification

Private mocks As MockRepository
Private view As IObjectSelectionView
Private domain As ObjectSelectionDomain

Private searchString As String

Protected Overrides Sub Context()
mocks = New MockRepository()

view = mocks.DynamicMock(Of IObjectSelectionView)()
domain = mocks.Stub(Of ObjectSelectionDomain)()
End Sub


Protected Overrides Sub Because()
searchString = "S"
End Sub


<Test()> _
Public Sub It_should_return_a_validationresult_errormessage()
Dim validationResult = New ValidationResult("Search String Too
Short!")

Using mocks.Record()
domain.GetObjects(Nothing, Nothing)
LastCall.Return(New List(Of ObjectDto)).OutRef
(validationResult)

view.ErrorMessages = validationResult.ErrorMessages
End Using

Dim presenter As ObjectSelectionPresenter = New
ObjectSelectionPresenter(view, domain)
presenter.GetObjects()

mocks.VerifyAll()
End Sub
End Class

In the call to "domain.GetObjects(?,?)" I want the OutRef to be the
validationResult
containing the given error message "Search String Too Short". But It
always returns
a ValidationResult without an error message.

Am I doing something obviously wrong here?


Thanks!

Tim Barcz

unread,
Nov 16, 2009, 7:23:16 AM11/16/09
to rhino...@googlegroups.com
Haven't compile checked it it appears from first glance that you never
get into replay mode. You need to get into replay mode (since you're
doing record/replay) for your return values to work.

Hope that helps. Let me know.

Tim
--
Sent from my mobile device

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

Tim Barcz

unread,
Nov 16, 2009, 7:27:24 AM11/16/09
to rhino...@googlegroups.com
Using (mocks.Playback) will get you into replay mode

devghost

unread,
Nov 16, 2009, 8:53:17 AM11/16/09
to Rhino.Mocks
Thanks Tim for your reply, I have corrected the test method to use the
Playback method.

<Test()> _
Public Sub It_should_return_a_validationresult_errormessage()
Using mocks.Record()
Dim validationResult = New ValidationResult("Search String
Too Short!")

domain.GetObjects(Nothing, Nothing)
LastCall.Return(New List(Of ObjectDto)).OutRef
(validationResult)

view.ErrorMessages = validationResult.ErrorMessages
LastCall.IgnoreArguments()
End Using

Using mocks.Playback()
Dim presenter As ObjectSelectionPresenter = New
ObjectSelectionPresenter(view, domain)
presenter.GetObjects()
End Using
End Sub

However, the problem still remains. When the presenter.GetObjects() in
turn call the domain.GetObjects(searchString, ByRef validationResult)
the ByRef (out) validationResult parameter does not contain the error
message that I wanted it to specified in the Record part.


Kind Regards,
Robert




On 16 Nov, 13:27, Tim Barcz <timba...@gmail.com> wrote:
> Using (mocks.Playback) will get you into replay mode
>
> On 11/16/09, Tim Barcz <timba...@gmail.com> wrote:
>
>
>
>
>
> > Haven't compile checked it it appears from first glance that you never
> > get into replay mode.  You need to get into replay mode (since you're
> > doing record/replay) for your return values to work.
>
> > Hope that helps. Let me know.
>
> > Tim
>

Alex McMahon

unread,
Nov 16, 2009, 8:59:37 AM11/16/09
to rhino...@googlegroups.com
I'm probably missing something, but don't you need:
LastCall.IgnoreArguments().Return(New List(Of ObjectDto)).OutRef
(validationResult)

is the call to domain.GetObjects in your presenter returning null? or the empty list you're specifying?

2009/11/16 devghost <rober...@gmail.com>

devghost

unread,
Nov 16, 2009, 9:05:59 AM11/16/09
to Rhino.Mocks

Indeed, that was it. Now it works as it should. Many thanks to
you both you saved my day (been spending a couple of hours to
get this to work with no luck).

Thanks!


On 16 Nov, 14:59, Alex McMahon <fluxmu...@gmail.com> wrote:
> I'm probably missing something, but don't you need:
> LastCall.*IgnoreArguments()*.Return(New List(Of ObjectDto)).OutRef
> (validationResult)
>
> is the call to domain.GetObjects in your presenter returning null? or the
> empty list you're specifying?
>
> 2009/11/16 devghost <robertbl...@gmail.com>

Tim Barcz

unread,
Nov 16, 2009, 9:07:30 AM11/16/09
to rhino...@googlegroups.com
One suggestion that would have helped...unless you must (and you shouldn't) use the AAA syntax over the RecordReplay.  It's much clearer and would've solved the "Playback" problem right out of the box.

Tim
--

devghost

unread,
Nov 16, 2009, 10:00:36 AM11/16/09
to Rhino.Mocks

I am taking a look at the AAA syntax and bumped into some issues
converting
the above test. This is what I have done:

Used shared GenerateMock, GenerateStub..
view = MockRepository.GenerateMock(Of IObjectSelectionView)()
domain = MockRepository.GenerateStub(Of ObjectSelectionDomain)
()

Modified the test to look like this, I think I got most of it to work
but I'm struggling
with the view...

<Test()> _
Public Sub It_should_return_a_validationresult_errormessage_aaa()
' Arrange
Dim validationResult = New ValidationResult("Search String Too
Short!")

' Act
domain.Stub(Function(e As ObjectSelectionDomain) e.GetObjects
(Nothing, Nothing)).IgnoreArguments().Return(New List(Of
ObjectDto)).OutRef(validationResult)

view.Expect(Function(e As IObjectSelectionView)
e.ErrorMessages = validationResult.ErrorMessages)

Dim presenter As ObjectSelectionPresenter = New
ObjectSelectionPresenter(view, domain)
presenter.GetObjects()

view.VerifyAllExpectations()
End Sub


The compiler doesn't like the way I call view.Expect with the write-
only ErrorMessages property. Is
there some other way to set a call expectation on write-only
properties? Can't seem to get it working.
Right now I really miss developing in C# :o)


Thanks!

Alex McMahon

unread,
Nov 16, 2009, 10:08:23 AM11/16/09
to rhino...@googlegroups.com
Indeed, I think you get much better responses here if you post in C#, I find it a real head trip translating VB...

A small point, but 'Arrange' would cover your stubbing as well. The only bit that would be counted as 'Act' is  presenter.GetObjects(). You would then put any assertions in a 'Assert' section.

You need to get rid of:

view.Expect(Function(e As IObjectSelectionView) e.ErrorMessages = validationResult.ErrorMessages
view.VerifyAllExpectations()

instead you should have view.Stub(x=>x....
and view.AssertWasCalled(x=>x....

It might feel like you're having to write a stub and an assert whereas before you just had an Expect, but it makes a lot more sense eventually. You should see it that Stub() is to support your test getting to the code you want to exercise (and not throwing). and that AssertWasCalled is your actual test.

2009/11/16 devghost <rober...@gmail.com>

devghost

unread,
Nov 17, 2009, 1:59:56 AM11/17/09
to Rhino.Mocks

It feels like I'm almost got it working now.. just one small thing,
this is what I got:

<Test()> _
Public Sub It_should_return_a_validationresult_errormessage_aaa()
' Arrange
domain.Stub(Function(e As ObjectSelectionDomain) e.GetObjects
(Nothing, Nothing)).IgnoreArguments().Return(New List(Of
ObjectDto)).OutRef(ValidationResult)
view.Expect(Function(x As IObjectSelectionView)
FakeErrorMessage(x, validationResult))

' Act
Dim presenter As ObjectSelectionPresenter = New
ObjectSelectionPresenter(view, domain)
presenter.GetObjects()

' Assert
view.VerifyAllExpectations()
End Sub

Private Function FakeErrorMessage(ByVal e As IObjectSelectionView,
ByVal validationResult As ValidationResult) As Boolean
e.ErrorMessages = ValidationResult.ErrorMessages
Return True
End Function


The above test works, the outref contains my error message. Since VB9
doesn't support anonymous
delegates I needed to use a fake method to put my expectations on.
However, if I am going to use
the way you proposed in replacing the Expect call with view.Stub, the
test looks like this:

<Test()> _
Public Sub It_should_return_a_validationresult_errormessage_aaa()
' Arrange
domain.Stub(Function(e As ObjectSelectionDomain) e.GetObjects
(Nothing, Nothing)).IgnoreArguments().Return(New List(Of
ObjectDto)).OutRef(ValidationResult)
view.Stub(Function(x As IObjectSelectionView) FakeErrorMessage
(x, validationResult))

' Act
Dim presenter As ObjectSelectionPresenter = New
ObjectSelectionPresenter(view, domain)
presenter.GetObjects()

' Assert
view.AssertWasCalled(Function(x As IObjectSelectionView)
FakeErrorMessage(x, validationResult))
End Sub


This test fails giving the error message
"IObjectSelectionView.set_ErrorMessages
(System.Collections.Generic.List`1[System.String]); Expected #1,
Actual #2.", meaning
my view.ErrorMessages writeonly property is called twice instead of
one time. Am i doing something wrong with Rhino Mocks, or might it be
my
usage of the fake method?


Thanks!



On 16 Nov, 16:08, Alex McMahon <fluxmu...@gmail.com> wrote:
> Indeed, I think you get much better responses here if you post in C#, I find
> it a real head trip translating VB...
>
> A small point, but 'Arrange' would cover your stubbing as well. The only bit
> that would be counted as 'Act' is  presenter.GetObjects(). You would then
> put any assertions in a 'Assert' section.
>
> You need to get rid of:
> view.Expect(Function(e As IObjectSelectionView) e.ErrorMessages =
> validationResult.ErrorMessages
> view.VerifyAllExpectations()
>
> instead you should have view.Stub(x=>x....
> and view.AssertWasCalled(x=>x....
>
> It might feel like you're having to write a stub and an assert whereas
> before you just had an Expect, but it makes a lot more sense eventually. You
> should see it that Stub() is to support your test getting to the code you
> want to exercise (and not throwing). and that AssertWasCalled is your actual
> test.
>
> 2009/11/16 devghost <robertbl...@gmail.com>

Alex McMahon

unread,
Nov 17, 2009, 4:44:54 AM11/17/09
to rhino...@googlegroups.com
Sorry, but I can't seem to get my head around the VBness of it all... I did read about having to use those fake methods, but not sure whether it could be the cause (although I doubt it). The first place I would look is whether the presenter is indeed setting the ErrorMessages twice.

2009/11/17 devghost <rober...@gmail.com>

devghost

unread,
Jan 8, 2010, 8:02:46 AM1/8/10
to Rhino.Mocks
Sorry for this late reply to this thread... But there was indeed no
problem
with the test, the ErrorMessages was set twice in the code.

Kind Regards
Robert


On 17 Nov 2009, 10:44, Alex McMahon <fluxmu...@gmail.com> wrote:
> Sorry, but I can't seem to get my head around the VBness of it all... I did
> read about having to use those fake methods, but not sure whether it could
> be the cause (although I doubt it). The first place I would look is whether
> the presenter is indeed setting the ErrorMessages twice.
>

> 2009/11/17 devghost <robertbl...@gmail.com>

> ...
>
> läs mer »

Reply all
Reply to author
Forward
0 new messages