我正在使用Rhino Mocks和nUnit一起嘗試在對象上測試函數IsApprovable()
。我正在測試的函數依賴於需要傳入的另一個對象「UserManager」。我試圖模擬UserManager的一個實例,以便我可以指定另一個函數的結果GetApproverDependantsList()
將模擬的依賴項傳遞給函數時發生InvalidCastException
我的問題是當我嘲笑對象並將其傳遞到我的測試,我得到以下InvalidCastException的功能:
無法轉換類型「Castle.Proxies.IUserManagerProxye15b431a53ca4190b7ffbdf5e241e2bb」的對象鍵入「MyNamespace.Users.UserManager」。
我是新來這個,所以我真的不知道,如果我正確做事......下面是示例代碼嘲笑我使用:
Dim helper As New BookingManagerHelper()
Dim booking As Booking = GetDummyBooking() 'method to get a booking in suitable state
Dim parameters As Collection(Of Parameter) = GetDummyParameters() 'factory method, as above
Dim mockedUserManager = MockRepository.GenerateMock(Of Users.IUserManager)()
'I have created a dummy function called GetUserCollectionWithDependant() to create the results I need the mocked function to return...
mockedUserManager.Stub(Function(x) x.GetApproverDependantsList(-1)).[Return](GetUserCollectionWithDependant(1))
'It's the line below where I find my exception...
Assert.AreEqual(True, helper.IsApprovable(booking, mockedUserManager, parameters))
我試圖測試函數看起來象以下內容:
Public Function IsApprovable(ByVal Booking As Booking , ByVal UserManager As Users.UserManager, Optional ByVal Parameters As Collection(Of Parameter) = Nothing) As Boolean
'various logic checks are performed on the objects passed in
End Function
需要注意以下幾點:
- 的UserManager實現接口IUserManager
- 的UserManager包含未由接口
- 的UserManager也從基類繼承定義的其他的屬性和功能(我需要重寫鹼特性?)
如果需要的話,我可以張貼更多的代碼。提前致謝。
謝謝!很簡單... – DazzledKid