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

what I would like to do is that a method of any object returns always true, like in this this minimal code :
public class MyClass
{
    public bool Verify()
    {
        MyClass2 myClass2 = new MyClass2();
        return myClass2.Check();
    }
}

public interface IMyInterface
{
    bool Check();
}

public class MyClass2 : IMyInterface
{
    public bool Check()
    {
        return false;
    }
}

class Program
{
    static void Main()
    {
        var fake = Isolate.Fake.Instance<IMyInterface>();
        Isolate.WhenCalled(() => fake.Check()).WillReturn(true);

        MyClass myClass = new MyClass();
        // I would like that Verify() returns true everytime, so how could I fake the Check() method to return always true ?
        Assert.AreEqual(true, myClass.Verify());
    }
}


But this is not working.
I can't refactor the code to put a IMyInterface in the myclass constructor parameter (or as method parameter).

How should I do ?

Thanks for your help,
Best regards

Edit : I tryed this too :

var fake = Isolate.Fake.Instance<IMyInterface>();
Isolate.WhenCalled(() => fake.Check()).WillReturn(true);
Isolate.Swap.AllInstances<IMyInterface>().With(fake);


but this shouldn't be the good way
asked by timor.super (1.9k points)

6 Answers

0 votes
Hi Timor,

We have a known issue of an exception thrown when Isolator is run from a console application.

Have you tried using the test code inside a test?
answered by dhelper (11.9k points)
0 votes
I've just done it, and I have this error :

TestCase 'TestProject1.UnitTest1.TestMethod1'
failed: TypeMock.TypeMockException: 
*** Can not call Swap.AllInstances() more than once on a type
   at TypeMock.ArrangeActAssert.AllSwapper`1.a(MockObject A_0)
   at TypeMock.ArrangeActAssert.SwapperBase`1.b(T A_0)
   C:	estcsharpConsoleApplication1TestProject1UnitTest1.cs(57,0): at TestProject1.UnitTest1.TestMethod1()


Is this the good way to mock this ?
answered by timor.super (1.9k points)
0 votes
Currently we do not support swapping of interfaces, I will add a feature request to Isolator backlog.

You can use the following code instead:
var fake = Isolate.Fake.Instance<MyClass2>(Members.CallOriginal);
Isolate.WhenCalled(() => fake.Check()).WillReturn(true);
Isolate.Swap.AllInstances<MyClass2>().With(fake);
var myClass = new MyClass();

Assert.AreEqual(true, myClass.Verify());
answered by dhelper (11.9k points)
0 votes
Great, this is working.

It could be easier to use the interface, if you have a lot of class implementing this one.
But it's already good.

thanks for your help
Best regards
answered by timor.super (1.9k points)
0 votes
I've another question.

How can I do, if Check method accepts an out parameters. and if I would like to fake the out parameter ?

Thanks for your help
answered by timor.super (1.9k points)
0 votes
ok, with DoInstead
but i've forgotten to add a reference to TypeMock API, so context.Parameters was innaccessible :oops:

sorry
answered by timor.super (1.9k points)
...