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

here is my scenario

i have a class A and some other classes that derived from A.
i need to mock a property of A and every time i instantiate a derived class that property on the derived class to be also mocked

can some1 help onm this?
asked by tolisss (28.8k points)

5 Answers

0 votes
Hi Tolisss,

I think what you're looking for is the CallBase method.
The example is taken from the documentation:


// C#
public class BaseClass
{
    public virtual int SomeMethod()
    {
        return 1;
    }
}

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

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


Let me know if this helps,
answered by gilz (14.5k points)
0 votes
hi Gilz

no imagine having a large number of derived classes what i want is to mock BaseClass.SomeMethod() and everytime i instanciate a Derived Class the SomeMethod also to be mocked

ps: i m not receiving any notification email from the forum that someone replied is there anything wrong with my account? i have also checked spam mails but nothing there also
answered by tolisss (28.8k points)
0 votes
Hi,

Unfortunately, we don't have such a feature. It is a good idea and a time saver, so we'll add it to our future feature list.

What can save you a bit of a pain is in the former example, use MockAll instead of Mock. This will mock the method to all instances of a derived class.

Mock mock = MockManager.MockAll(typeof(DerivedClass)) ;
answered by gilz (14.5k points)
0 votes
Thanks for the heads up, I'm checking the e-mail notification issue.
answered by avid (180 points)
0 votes
hi still nothing on the e-mail notification issue. :(
answered by tolisss (28.8k points)
...