How to set return value for readonly property using RhinoMocks in VB.NET?

I am using RhinoMock in VB.NET and I need to set the return value for a read-only list.

Here is what I want to do (but not working):

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

Compilation fails because Posts is a readonly property.

Then I tried a lambda expression that works great for function calls, but not so much for properties. This does not compile.

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

The next attempt (unsuccessful attempt) was to use SetupResults, but this did not confirm 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 to set return value for readonly property using RhinoMocks in VB.NET?

+3
1

IUserDto ? , . , , readonly . RhinoMocks /, .

() , :

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

. 10, ( , ), Class1.Test.

, ( , RhinoMocks VB.NET ).

+1

Source: https://habr.com/ru/post/1711920/


All Articles