R – How to setup return value for a readonly property using RhinoMocks in VB.NET

rhino-mocksvb.net

I'm using RhinoMock in VB.NET and I need to set the return value for a readonly list.

Here's what I want to do (but doesn't work):

dim s = Rhino.Mocks.MockRepository.GenerateStub(of IUserDto)()
s.Id = guid.NewGuid
s.Name = "Stubbed name"
s.Posts = new List(of IPost)

It fails on the compile because Posts is a readonly property.

Then I tried a lambda expression, which works fine for Function calls, but not so much for Properties. This fails to compile.

s.Stub(Function(x As IUserDto) x.Posts).Return(New List(Of IPost))

Next (failing) attempt was to use SetupResults, but this failed stating that it cannot be used in Playback mode.

Rhino.Mocks.SetupResult.For(s.Posts).Return(New List(Of IPost))

Which brings me back to my question:

How do I setup a return value for a readonly property using RhinoMocks in VB.NET?

Best Solution

Is IUserDto an interface? If it is then it should just work. If it isn't, then the problem could be that the readonly property in question is not overridable. RhinoMocks can only mock properties/methods which are defined in an interface or can be overridden.

Here is my (clumsy) attempt at proving that the lambda syntax should work:

Imports Rhino.Mocks

Public Class Class1

    Public Sub Test()
        Dim s = MockRepository.GenerateMock(Of IClass)()
        Dim newList As New List(Of Integer)

        newList.Add(10)

        s.Stub(Function(x As IClass) x.Field).Return(newList)

        MsgBox(s.Field(0))

    End Sub

End Class

Public Class AnotherClass
    Implements IClass

    Public ReadOnly Property Field() As List(Of Integer) Implements IClass.Field
        Get
            Return New List(Of Integer)
        End Get
    End Property
End Class

Public Interface IClass
    ReadOnly Property Field() As List(Of Integer)
End Interface

i.e. I would get a message box with the number 10 displayed on it (I didn't bother to try hooking up a unit-testing framework with this but that shouldn't make a difference) when Class1.Test is invoked.

Hope that helps (it was an interesting exercise trying to work with RhinoMocks in VB.NET in anycase).

Related Question