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
0 votes
Hi, There

I have a target class that I need to test. The parent of this target class provides some database interfacing functions that I want to mock. I can't mock the target class, because I need to test it. But is there a way to mock the behavior of its parent methods?

Thanks.


Richard Zhu
asked by rzhu (2.8k points)

5 Answers

0 votes
Hi Richard,

Yes, there is a method for mocking parent class functionality only. its done using the CallBase API look here for more details: https://www.typemock.com/Docs/UserGuide/ ... lBase.html

Also I'm not sure if this is applicable in your case, but you can mock out only parts of a tested class i.e. mock out just specific methods on a given instance. you might find that also useful in your case.
answered by lior (13.2k points)
0 votes
Hi, Lior

Thanks for the information. I looked at the CallBase API, the document says this is used for when you want to mock the base implementation of a overridden method. This is different from my case in 2 aspects:

1. I don't want to mock the child class, because it is the class I want to test.
2. The child class does not override the parent method.

Can you tell me more about mocking specific method of a class? Where is it in the help file? I might be able to use that for my scenario. If I can mock my target class, and let the class handle the method that I want to test, but mock methods inherited from parent.

Thanks


Richard
answered by rzhu (2.8k points)
0 votes
Hi Richard,

Sorry, I misunderstood what you wanted.

In general a small example demonstrating what you need will go a long way in helping us understand you better.

Anyway, here is an example that shows how to mock a specific method from on a given instance. (In this example the method is declared on the parent class, but this will work on on all other methods as well)

public class BaseClass
{
    public int MethodInBase()
    {
        return 5;
    }
}

public class UnderTest : BaseClass
{
    public int OtherMethod()
    {
        return 6;
    }
}

[TestMethod]
[VerifyMocks]
public void MockingSpecificMethod_AndExecutingTheRestNormally()
{
    UnderTest target = new UnderTest();
    using (RecordExpectations rec = new RecordExpectations())
    {
        target.MethodInBase();
        rec.Return(10);
    }

    Assert.AreEqual(10, target.MethodInBase());
    Assert.AreEqual(6, target.OtherMethod());
}


As you can see only the call MethodInBase was mocked while all other calls will go to real code. By default this is the expected behavior for concrete classes.
:!: This behavior will change if you are mocking interfaces.

Anyway I hope this is what you meant. if not please post a short example and I'm sure we will manage to find a good answer for you.
answered by lior (13.2k points)
0 votes
Hi, Lior

This looks like exactly what I want it to be. Thanks for the help. Can you also post an example in Reflective Mock instead of Natural Mock? I have all my other code in Reflective Mock and would like to keep the style consistent.

Thanks.


Richard
answered by rzhu (2.8k points)
0 votes
Try this:

[TestMethod] 
[VerifyMocks] 
public void MockingSpecificMethod_AndExecutingTheRestNormally() 
{ 
    MockObject mock = MockManager.MockObject<UnderTest>(Constructor.NotMocked);    
    mock.ExpectAndReturn("MethodInBase",10);

    UnderTest target = mock.Object as UnderTest; 
    Assert.AreEqual(10, target.MethodInBase()); 
    Assert.AreEqual(6, target.OtherMethod()); 
}
answered by lior (13.2k points)
...