Search code examples
c#unit-testingmockingtypemocktypemock-isolator

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


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?


Solution

  • 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());
    
       }
    

    `