chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
Welcome to Typemock Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
0 votes
Hi all,

How does TypeMock deal with casting?

For example:

My Code

...
myClass
{
private IToyota _myToyota;

...

IVehicle myVehicle;
myVehicle = _myToyota as IVehicle;

if(myVehicle != null)
     myVehicle.ShutDownEngine();

...
}


Now here is the test code I would write:
MockObject mockToyota = MockManager.MockObject(typeof(IToyota));
myClassAccessor._myToyota = mockToyota.object as IToyota;



My questions are:

1) Whenever my code under test runs to the casting as IVehicle, myVehicle is null because the casting did not succeed, and I cannot check if ShutDownEngine() is called. How would I mock such a casting?

2) How would I expect a call to ShutDownEngine()?

Any suggestions would be greatly appreciated!

Thanks,

YFC
asked by YFC (1.2k points)

1 Answer

0 votes
hi,
can you post the declaration of IToyota and IVehicle?

is one interface implementing the other?
If not you have two options:
1) create a new interface (just for testing) that implements both interfaces and mock it passing i to the tested class.

2) mock the concrete class that is used in production and implements both interfaces.
answered by lior (13.2k points)
...