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

I have a class called Test in which I will be creating instance of other class in one of the method as show below

Class Test
{

Public void SomeMethod()
{
ITest1 test = new Test1()
}
}
Interface ITest1
{
}
Class Test1 : ITest1
{
}
Now How to mock \" ITest1 test = new Test1()\" so that when SomeMethod gets called it will not call the real implementation of Test1.

Thanks,
Praveen V
asked by (6.1k points)
edited by

1 Answer

+1 vote

Hi,

Please see the following example:
 

[TestClass]
public class UnitTest1
{
    [TestMethod]
    public void TestMethod1()
    {
        var fake = Isolate.Fake.AllInstances<ITest1>();
        Isolate.WhenCalled(() => fake.GetNum()).WillReturn(5);

        var num = fake.GetNum();

        Assert.AreEqual( 5 , num);
    }
}

class Test
{
    public void SomeMethod()
    {
        ITest1 test = new Test1();
    }
}

interface ITest1
{
    int GetNum();
}

class Test1 : ITest1 
{
    public int GetNum()
    {
        throw new NotImplementedException();
    }
}


"fake" is basically a proxy to the instance created in SomeMethod and the behavior that's applied to it will apply to "test".

You can see more examples once you download Isolator. You can download for free here: https://www.typemock.com/download/

Let me know if it helps.

answered by (18k points)
edited by
...