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,

Thanks for any help you can give me, I'm probably missing something simple here, but I'm trying to do the following:

    public class BaseClass
    {
        public int SomeMethod()
        {
            return 1;
        }
    }

    public class DerivedClass : BaseClass
    {
        public int SomeMethodNew()
        {
            return base.SomeMethod() + 1;
        }
    }

        [TestMethod]
        public void Test()
        {
            Mock<DerivedClass> mock = MockManager.Mock<DerivedClass>();
            // mock only BaseClass.SomeMethod when called from a DerivedClass Type.
            mock.CallBase.ExpectAndReturn("SomeMethod", 100);
            DerivedClass d = new DerivedClass();
            int result = d.SomeMethodNew();
            Assert.AreEqual(101, result);
        }


I've modified the sample for the CallBase documentation to call a different method name rather than an overriden method.

This doesn't work though, so how do i mock out the call to the base class? I've tried mocking the base class directly but this doesn't seem to work either.

        [TestMethod]
        public void Test()
        {
            Mock<BaseClass> mock = MockManager.Mock<BaseClass>();
            mock.ExpectAndReturn("SomeMethod", 100);
            DerivedClass d = new DerivedClass();
            int result = d.SomeMethodNew();
            Assert.AreEqual(101, result);
        }

Any Ideas??
Cheers,
Rob
________
Super Cub
asked by rob@adactus.co.uk (640 points)

2 Answers

0 votes
Hi
This is a documented behavior.
From the 'Other Issues' section in Isolator documentation:
When mocking Base, the foo method will be mocked ONLY for objects that are Base. Calls to foo from objects that are Derived will not be mocked.


When you use MockManager.Mock try to think of it as
Next time there will be call to 'new SomeType()' it will be mocked
Although the constructor for BaseClass is called implicitly when you are creating DerivedClass the Isolator does not mock the base class in this case.
answered by ohad (35.4k points)
0 votes
Thanks for your reply Ohad.

I actually managed to get the example i posted working in the end. It turned out i was referencing an old version of typemock.

Cheers,
Rob
________
synthetic weed
answered by rob@adactus.co.uk (640 points)
...