"Cannot verify on real object - use a fake object instead" exception

123 Views Asked by At

I'm using Typemock Isolator version 8.6.2.0. I have the following classes:

public class A
{
    public B b { get; }

    public A()
    {
        b = new B();
    }
}

public class B
{
    public B()
    {
        Console.WriteLine("In B c'tor");
    }
    public void doSomething()
    {

    }
}

The test method is:

public void test()
{
    Isolate.Fake.NextInstance<B>();
    A a = new A();
    var bObject = a.b;
    bObject.doSomething();
    Isolate.Verify.WasCalledWithAnyArguments(() => bObject.doSomething());
}

When I run the test I get the following exception:"Cannot verify on real object - use a fake object instead", but the object is faked! Does anyone know why it happens and how I can fix it?

2

There are 2 best solutions below

0
AudioBubble On BEST ANSWER

write your test like this: `

   public void test()
   {
       var fake = Isolate.Fake.NextInstance<B>();
       A a = new A();
       var bObject = a.b;
       bObject.doSomething();
       Isolate.Verify.WasCalledWithAnyArguments(() => fake.doSomething());

   }

`

0
Hans Kilian On

NextInstance returns a handle that you can call Verify on. Right now, you throw the returned handle away.

Per the docs at http://www.typemock.com/docs/?book=Isolator&page=Documentation%2FHtmlDocs%2Ffakingfutureinstances.htm

Verifying is done on the instance returned from Isolate.Fake.NextInstance.