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 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!
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 (3.1k 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 (12k points)
...