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
The codez:
    public class ClassToTest
    {
        public void TestMe()
        {
            var youllBeFaked = new ClassToFake();
            youllBeFaked.VerifyMe();
        }
    }

    public class ClassToFake
    {
        public void VerifyMe()
        {
            throw new Exception("I wasn't faked!");
        }
    }


The test:
    public class TestBase
    {
        protected ClassToFake fake { get; private set; }

        [SetUp]
        public void SetUp()
        {
            MockManager.Init();
            fake = Isolate.Fake.Instance<ClassToFake>();
            Isolate.Swap.AllInstances<ClassToFake>().With(fake);
        }

        [TearDown]
        public void TearDown()
        {
            MockManager.Verify();
            MockManager.ClearAll();
        }
    }
    
    [TestFixture]
    public class ClassToTestTest : TestBase
    {


        /// <summary>
        ///A test for TestMe
        ///</summary>
        [Test]
        public void TestMeTest()
        {
            ClassToTest target = new ClassToTest();
            target.TestMe();
            Isolate.Verify.WasCalledWithAnyArguments(() => fake.VerifyMe());
        }
    }


The exception:
TypeMock.TypeMockException :
*** Isolate.Verify does not support objects that were not faked using Isolate.Fake.Instance(), or passed through WhenCalled()
at cp.a()
at du.a()
at db.a(Boolean A_0)
at dh.b(Boolean A_0)
at b3.b(Delegate A_0)
at b3.b(Action A_0)
at SomeProjectTest.ClassToTestTest.TestMeTest() in ClassToTestTest.cs: line 54

What am I doing wrong?

Thx for the help.
asked by Guillaume (3.5k points)

4 Answers

0 votes
Hi,

Does the same test work when the setup/teardown are in the same test class, rather than a base class?

Also, Instead of using MockManager init/Verify/Clear, you should use the Isolated attribute on the test method or class.

HTH
Doron
answered by doron (17.2k points)
0 votes
No, the test does not work any better if I move the Setup to ClassToTestTest and remove the inheritance. The same problem occurs.

I also removed any call to MockManager and added the Isolated tag. It made no difference.

Here's the new, modified code for the test

    [TestFixture]
    public class ClassToTestTest
    {
        
        protected ClassToFake fake { get; private set; }

        [SetUp]
        public void SetUp()
        {
            fake = Isolate.Fake.Instance<ClassToFake>();
            Isolate.Swap.AllInstances<ClassToFake>().With(fake);
        }

        [Test]
        [Isolated]
        public void TestMeTest()
        {
            ClassToTest target = new ClassToTest();
            target.TestMe();
            Isolate.Verify.WasCalledWithAnyArguments(() => fake.VerifyMe());
        }
    }
answered by Guillaume (3.5k points)
0 votes
Odd - that should work. Would it be possible for you to send us the sample project to support(at)typemock.com? I'd rather recreate this offline to find out what happened.
answered by doron (17.2k points)
0 votes
Hi Guillaume,

The short answer: Use a field instead of a property:
protected ClassToFake fake;

The longer answer:

Using Isolate.Fake.Instance, you created a fake for your ClassToFake. But, in your Verify statement, the lambda expression used the getter method of your Tests clsas, generated by the auto-property:
() => this.get_fake().VerifyMe()

Tests class wasn't faked ofcourse, thus the exception.
If you still prefer using a property, you can extract the faked object prior to the Verify statement:
var fakeObject = fake;
Isolate.Verify.WasCalledWithAnyArguments(() => fakeObject.VerifyMe());

_________________
Regards

Yonatan,
TypeMock Support Group
answered by yonatan (1.6k points)
...