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
Hello,

I am wondering if this is possible, tried it with version 3.7 and couldn't succed. I want to return an existing object instance of the return value of a mocked constructor call. To give example, the code to be tested is:

......
Class1 a = new Class1();
a.SomeMethod();
....


this is the tester code:

Class1 ourInstance = new Class1();
....
// initialize properties etc on ourInstance
...

using (RecordExpectation rec = RecorderManager.StartRecording())
{
Class1 c = new Class1();
rec.Return(ourInstance);
}


What we are trying to do, is to have variable "a" in the code being tested to be assigned to the instance ourInstance when the constructor for it being called, which gets mocked. But it appears the rec.Return(ourInstance) statement doesn't have any effect here.

Is it possible to return an instance of our choice as the result of a mocked constructor call?

TIA
asked by phazer (4k points)

1 Answer

0 votes
Hi
Currently we cannot do this. The RecordExpectation.Return method will not work on constructors.
However If you'll create GetInstance method you'll be able to do that.

class Class1
{
   public Class1 GetInstance() 
   {
      ...
   }
}

using (RecordExpectation rec = RecorderManager.StartRecording()) 
{
   Class1 c = new Class1();
   c.GetInstance();
   rec.Return(ourInstance)
}
answered by ohad (35.5k points)
...