Note that there are some explanatory texts on larger screens.

plurals
  1. POInvalidCastException when passing a mocked dependency into a function
    primarykey
    data
    text
    <p>I am using Rhino Mocks along with nUnit to attempt to test my function <code>IsApprovable()</code> on an object. The function I am testing relies on another object "UserManager" that needs to be passed in. I am attempting to mock an instance of UserManager so I can specify the result of another function <code>GetApproverDependantsList()</code></p> <p>My issue is that when I mock the object and pass it into the function I am testing I get the following InvalidCastException:</p> <p><strong>Unable to cast object of type 'Castle.Proxies.IUserManagerProxye15b431a53ca4190b7ffbdf5e241e2bb' to type 'MyNamespace.Users.UserManager'</strong>.</p> <p>I am new to this so I am not really sure if I am doing things correctly... Here is the sample mocking code I am using: </p> <pre><code>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)) </code></pre> <p>The function I am attempting to test looks like the following:</p> <pre><code>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 </code></pre> <p>Some things to note: </p> <ul> <li>UserManager implements the interface IUserManager</li> <li>UserManager contains additional properties and functions that are not defined by the interface</li> <li>UserManager also inherits from a base class (do I need to override base properties?)</li> </ul> <p>I can post more code if needed. Thanks in advance.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload