Rhino mocks 3.3 casting mock interface to parent interface

554 views
Skip to first unread message

Matthew Evans

unread,
Jun 30, 2009, 2:10:54 AM6/30/09
to Rhino.Mocks
I have a small issue i havent been able to find any documentation on.
Using 3.3 I am creating a mock object say IMyObject.
IMyObject inherits from IMyParentObject. Now somewhere in the code the
mock object is being cast as IMyParentObject and return a null.

My question is how do I prepare a mock to deal with castings implicit
and explicit?
Thanks.

Ayende Rahien

unread,
Jun 30, 2009, 2:33:20 AM6/30/09
to Rhino...@googlegroups.com
Please create a test case for this, what you describe should not be possible

Matthew Evans

unread,
Jul 1, 2009, 12:21:19 AM7/1/09
to Rhino.Mocks
My apologies seems what i was looking for was the use of
DynamicMultiMocks<T>().

However switching to this presented another problem, I'm mocking an
Interface that inherits from the System.ServiceModel.IClientChannel
and using the DynamicMultiMocks<T>(param Type[]) call results in the
BadImageFormatError that seems to be quite common. Now the alternative
present for a single remote mock works fine but it brings me back to
my original problem getting a null when the Mock is cast as the parent
Interface.

Is there an alternative RemoteMultiMocks<T>(param Type[]) or work
around for this currently?

On Jun 30, 6:33 pm, Ayende Rahien <aye...@ayende.com> wrote:
> Please create a test case for this, what you describe should not be possible
>

Kenneth Xu

unread,
Jul 1, 2009, 12:31:25 PM7/1/09
to Rhino...@googlegroups.com
Matthew,
As Ayende said, show us the code to reproduce this so we can help. I
also believe what you described is not possible.
Cheers,
Kenneth

Matthew Evans

unread,
Jul 6, 2009, 12:35:20 AM7/6/09
to Rhino.Mocks
The IOnlineAVLVehicleService interface inherits from
System.ServiceModel.IClientChannel.
This current configuration throws a BadImageFormat error, where as I
am unaware of any alternative to create multiple remote mocks.


[TestMethod] //Commmunication error on the GetVehicleService
public void
IsGeofenceEventsDisplayedOnMDT_ServiceTimeoutException_InvalidTest()
{
bool isGeofenceEventsDisplayedOnMDT = false;
MockRepository mock = new MockRepository
();
IProxyManager proxyManager =
mock.DynamicMock<IProxyManager>();
IOnlineAVLVehicleService vehicleService =
mock.CreateMultiMock<IOnlineAVLVehicleService>(new Type[] { typeof
(IOnlineAVLVehicleService), typeof(IClientChannel) });
VehicleInterface.GetVehicleDetailResponse vehicleDetailResponse =
GenerateVehicleDetailResponse(false, true);

using (mock.Record())
{
Expect.Call(vehicleService.GetVehicleDetail(new
VehicleInterface.GetVehicleDetailRequest())).IgnoreArguments().Throw
(new TimeoutException());
Expect.Call(proxyManager.GetVehicleService()).Return
(vehicleService).Repeat.Any();
}

using (mock.Playback())
{
isGeofenceEventsDisplayedOnMDT =
GeofenceUtilities.IsGeofenceEventsDisplayedOnMDT(Guid.NewGuid(),
proxyManager);
}

Assert.IsFalse(isGeofenceEventsDisplayedOnMDT);
}



public static bool IsGeofenceEventsDisplayedOnMDT(Guid vehicleID,
IProxyManager proxyManager)
{
if (proxyManager == null)
{
throw new ArgumentNullException("proxyManager");
}

if (vehicleID == Guid.Empty)
{
throw new ArgumentException(string.Format("'vehicleID'
must be a valid vehicle id and not '{0}'", Guid.Empty.ToString()));
}

bool
showPolygonalGeofenceEventsOnMDT =
false;
VehicleInterface.GetVehicleDetailRequest
getVehicleDetailRequest = new
VehicleInterface.GetVehicleDetailRequest();

getVehicleDetailRequest.VehicleID =
vehicleID;
VehicleInterface.GetVehicleDetailResponse
vehicleDetailResponse = null;
VehicleInterface.IOnlineAVLVehicleService
vehicleService = proxyManager.GetVehicleService();

Exception exceptionXXXYZ = ChannelInvoker.Invoke(delegate
(out IClientChannel channel)
{
channel = vehicleService as IClientChannel;
vehicleDetailResponse = vehicleService.GetVehicleDetail
(getVehicleDetailRequest);
});

if (exceptionXXXYZ == null)
{
if (vehicleDetailResponse.OperationStatus)
{
showPolygonalGeofenceEventsOnMDT =
vehicleDetailResponse.VehicleInfo.NotifyDriverOfGeofenceAlert;
}
else
{
throw new ApplicationException("Cannot get vehicle
detail for vehicle:" + getVehicleDetailRequest.VehicleID.ToString());
}
}
else
{
ExceptionManager.HandleException(exceptionXXXYZ,
getVehicleDetailRequest);
}

return showPolygonalGeofenceEventsOnMDT;
}

Krzysztof Koźmic (2)

unread,
Jul 6, 2009, 5:14:01 AM7/6/09
to Rhino.Mocks
What is the exact error message and stacktrace you get in this
exception?

Kenneth Xu

unread,
Jul 6, 2009, 12:03:01 PM7/6/09
to Rhino...@googlegroups.com
Matthew,

BadImageFormat aside, if IOnlineAVLVehicleService inherits from
IClientChannel, why do you need to use MultiMock? CreateMock should be
good to go.

IOnlineAVLVehicleService vehicleService =
mock.CreateMultiMock<IOnlineAVLVehicleService>();
IClientChannel clientChannel = vehicleService;
Assert.IsNotNull(clientChannel); // this should pass!

Cheers,
Kenneth

On Mon, Jul 6, 2009 at 12:35 AM, Matthew Evans<climba...@gmail.com> wrote:
>
> The IOnlineAVLVehicleService interface inherits from
> System.ServiceModel.IClientChannel.
> This current configuration throws a BadImageFormat error, where as I
> am unaware of any alternative to create multiple remote mocks.
>

Matthew Evans

unread,
Jul 6, 2009, 6:41:37 PM7/6/09
to Rhino.Mocks
I apologize the issue was not within Rhino mocks but rather the
projects inner framework.
Sorry for wasting your time.

Tim Barcz

unread,
Jul 7, 2009, 12:44:30 PM7/7/09
to Rhino...@googlegroups.com
That's why we test our code....we've all been there.

Thanks for letting us know...

Tim
--
Tim Barcz
ASPInsider
http://timbarcz.devlicio.us
http://www.twitter.com/timbarcz

Reply all
Reply to author
Forward
0 new messages