2009-07-06 64 views
3

我在VB.NET中使用RhinoMock,我需要設置只讀列表的返回值。如何在VB.NET中使用RhinoMocks爲readonly屬性設置返回值?

這裏就是我想做(但不工作):

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

它失敗的編譯,因爲文章是一個只讀屬性。

然後我嘗試了一個lambda表達式,該函數可以很好地用於函數調用,但對於屬性沒有太大影響。這不能編譯。

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

接下來(失敗)的嘗試是使用SetupResults,但是失敗說明它不能在回放模式下使用。

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

這讓我回到我的問題:

如何設置在VB.NET使用RhinoMocks一個只讀屬性,返回值?

+0

爲什麼Rhino.Mocks.SetupResult.For(s.Posts).Return(New List(Of IPost))失敗? – Grzenio 2009-07-07 08:36:25

回答

1

IUserDto的界面嗎?如果是的話,它應該只是工作。如果不是,那麼問題可能是所討論的只讀屬性不可覆蓋。 RhinoMocks只能模擬在接口中定義的或可被覆蓋的屬性/方法。

這裏是一個證明的lambda語法應工作我(笨拙)嘗試:

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時。

希望有幫助(這是一個有趣的嘗試在任何情況下與VB.NET中的RhinoMocks合作)。

相關問題