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
Welcome to Typemock Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
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 by hho (600 points)

1 Answer

0 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 by Elisha (12k points)
...