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 Answers. Here you can ask and receive answers from other community members. And if you liked or disliked an answer or thread: react with an up- or downvote Enjoy!
0 votes
Hello,

I am trying to mock calls to an object instantiated by CreateInstanceAndUnwpap, i.e. the object is created by a factory in a different domain.

It does not seem to work. I have a code like this:

IMyInterface obj = MyFactory.GetObject();
using(RecordExpectations recorder = RecorderManager.StartRecording())
{
obj.DoSomething();
}

A call to DoSomething never gets mockes, i.e. the original method is called. Can this be because MyFactory.GetObject() returns an object of a type __TransparentProxy? And if this is case, is there anything I can do about it?
asked by (19.4k points)

6 Answers

0 votes
You are correct, the __TransparentProxy is in mscorlib, and it is not possible to mock.
We should actually support this scenario. I will add it as a feature to a future version of TypeMock.

Thanks for pointing out this scenario
answered by (32k points)
0 votes
You could in this case mock the IMyInterface, and thus isolate the remote object.

IMyInterface obj = RecorderManager.CreateMockedObject
   (typeof(IMyInterface)) as IMyInterface ;
using(RecordExpectations recorder = RecorderManager.StartRecording())
{
   obj.DoSomething();
} 
answered by (32k points)
0 votes
Thanks, that's a good idea.

I am trying this now, and I get the following exception:

TypeMock Verification: Unexpected Call to MockIMyInterface.DoSomething()
answered by (19.4k points)
0 votes
You will have to mock all the calls.
use
recorder.RepeatAlways()


and use the Tracer to check it.
answered by (32k points)
0 votes
I am getting a weird problem now. In my code prior to interface mocking (as you suggested) I have an innocent call that on return assigns a value to an object. Something like this:

SomeCustomType t = a.GetCustomObject();
IMyInterface obj = RecorderManager.CreateMockedObject(typeof(IMyInterface)) as IMyInterace;
using (RecordExpectations recorder = RecorderManager.StartRecording())
{
obj.DoSomething();
}

Then I get a TypeMock exception "unexpected call to DoSomething". But if I remove assignment to "t" in the first line and just call GetCustomObject as if it was void method, so first line looks like this:

a.GetCustomObject();

then the rest goes fine and I don't get TypeMock exception about unexpected call.

I wonder what can this be?
answered by (19.4k points)
0 votes
Well I understand why I had this weird problem with unexpected call. I was debugging the project and VS "Local" window was active. So .NET evaluated all local objects 'causing them to be mocked. Once I closed "Locals" window things went back to normal.
answered by (19.4k points)
...