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!

CallBase.ExpectCall not work as expected.

0 votes
Hi,

I try to check if the base method was called. I try to use the CallBase.ExpectCall method, it doesn't work as expected. However if I use CallBase.ExpectAndThrow method, an exception was throw as expected. Please Advice.

public class BaseClass
        {
            public int someValue = 0;

            public virtual void Initialize()
            {
                someValue++;
            }
        }

        public class DerivedClass : BaseClass
        {
            public override void Initialize()
            {
                /* The Unit test is trying to check if the base.Initialize() was called */
                // base.Initialize();
                someValue++;
            }
        }

        [TestMethod]
        public void Test()
        {
            Mock<DerivedClass> mock = MockManager.Mock<DerivedClass>();
            // mock only BaseClass.SomeMethod when called from a DerivedClass Type.
            mock.CallBase.ExpectCall("Initialize");
            DerivedClass d = new DerivedClass();
            d.Initialize();
            // This always passed, expected to fail since the Initialize method in the base was not called after it being commented
            mock.Verify();
        }
asked Mar 21, 2012 by hho (120 points)

1 Answer

+2 votes
Hi,

This is a bug, Mock.Vertify verifies only the instance calls and not the base class calls.

As a workaround, you can use the VerifyMocks attribute:

[TestMethod, VerifyMocks]
public void Test()
{
         Mock<DerivedClass> mock = MockManager.Mock<DerivedClass>();
         // mock only BaseClass.SomeMethod when called from a DerivedClass Type.
         mock.CallBase.ExpectCall("Initialize");
         DerivedClass d = new DerivedClass();
         d.Initialize();
}


Regards,
Elisha,
Typemock Support
answered Mar 29, 2012 by Elisha (22,740 points)
...