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
0 votes
Hi everyone,

I am totally stuck at this situation: I am mocking an instance of a custom interface:

IProduct5 product = Isolate.Fake.Instance<IProduct5>();

Now, I'm querying another interface from it:

ITaSecurity6 iTaSecurity6 = product.taSecurity;

And here comes the problem, when casting this ITaSecurity6 instance to ITaSecurity12:

ITaSecurity12 iTaSecurity12 = (ITaSecurity12)iTaSecurity6;

"System.InvalidCastException: Unable to cast object of type 'Mock0001ITaSecurity6' to type 'ITaSecurity12'.

How can I mock this ITaSecurity12 instance?

Thanks in advance!
asked by Treufeld (720 points)

6 Answers

0 votes
Hello,

Could you please post the interface declarations of ITaSecurity6 and ITaSecurity12? Does ITaSecurity12 implement ITaSecurity6?

Also, is product.taSecurity a field or a property?
answered by igal (5.7k points)
0 votes
Hi,


>Does ITaSecurity12 implement ITaSecurity6?

Yes, ITaSecurity12 contains all the methods of ITaSecurity6, and some more. Product.TaSecurity is a read-only property. Actually all the interfaces I mentioned before are COM interfaces which are imported into .NET, so typecasting ITaSecurity6 into ITaSecurity12 means a QueryInterface() call.
answered by Treufeld (720 points)
0 votes
Hi,

Just to make sure - is there actual inheritance relationship between ITaSecurity6 and ITaSecurity12? Is ITaSecrity12 derived from ITaSecurity6? Isolator creates a derived dynamic class from the faked interface. If the interface is derived from another interface, the dynamic class will be compatible with both. This is more iffy isf the interfaces are only related by unmanaged runtime.

Doron
Typemock Support
answered by doron (17.2k points)
0 votes
Hi,

No, ITaSecurity6 and ITaSecurity12 are both inherited from the IDispatch COM interface. I am sure someone could help who has been dealing with COM interop and TypeMock :)
answered by Treufeld (720 points)
0 votes
OK, the limitation in Isolator in this case is this - because Isolator dynamically creates a class based on ITaSecurity12, it cannot be then cast to ITaSecurity6, without there being inheritance relationships between them.

Try the following: create a class that derives from both interfaces, and use that in your tests. This should be able to be case either way:
public class TaSecurityForTests : ITaSecurity6, ITaSecurity12 {...}

var fakeTaSecurity = Isolate.Fake.Instance<TaSecurityForTests>(); // pass this to your code under test


Let me know if this is any help.

Doron
Typemock Support
answered by doron (17.2k points)
0 votes
Thanks a lot! Now it started working this way.[/code]
answered by Treufeld (720 points)
...