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

I have an interface and one of the functions of the interface is explicitly defined in the derived class. In my function which is to be tested, I am calling that function of the interface:

public Interface IManager
{
int ReturnInteger();
}

public class Manager:IManager
{
int IManager.ReturnInteger()
{
return 20;
}
}

public class Person
{
int ReturnValue()
{
IManager m = new Manager();
return m.ReturnInteger();
}


In the above code, I have to mock the ReturnInteger function of the IManager class. Somebody please suggest how can I do this.

Thanks and Regards
asked by divya.sarin (600 points)

1 Answer

0 votes
Casting seems to work:

[Test]
public void MyTest()
{
  using(RecordExpectations recorder = RecorderManager.StartRecording())
  {
    Manager dummy = new Manager();
    int fakeReturn = ((IManager)dummy).ReturnInteger();
    recorder.Return(7);
  }
  Person p = new Person();
  Assert.AreEqual(7, p.ReturnValue());
}
answered by tillig (6.7k points)
...