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 have a question about how to mock base methods of a mustinherit class (our code is written in vb.net)

Here is an example that illustrates my problem
This example should be compilable so that you can actually test it out and see what I am taking about, assuming you import typemock and nuint.

This is the mustinherit (abstract) class:

Public MustInherit Class DataObjectProof

    Public Overridable Sub Save(ByVal sLogger As String)
            Throw New Exception("Just proving a point: " & sLogger)
    End Sub
End Class


This class inherits the mustinherit class and overloads a method defined in the parent class:

Public Class Message
        Inherits DataObjectProof

        Public Overloads Sub Save(ByVal sLog1 As String, ByVal sLog2 As String)
            Console.WriteLine(sLog1)
            MyBase.Save(sLog2)
        End Sub
    End Class


And finally, this is my test:

<TestFixture(), Category("DDU Tests")> Public Class MessageTest

        <Test(), Category("MessageTest")> _
        Public Sub MessageTester()
            Dim oMessage As New Message

            MockManager.Init()

            Using oRecorder As New RecordExpectations
                oMessage.Save(Nothing)
            End Using

            oMessage.Save("testing out mocking base class method", "I am about to throw an exception")

            MockManager.Verify()


        End Sub


This test passes when run, when I really want the exception to be thrown and the test fail (just for this example)
What I want to happen is that when the overloaded method calls mybase.save, the save of the mustinherit class is mocked, and not the save on the child class.

Is this possible with typemock? And if it is, how do I go about doing this?
The developer guide hints that this isn't possible:

Derived Classes and Overloaded Methods Behavior

The Typemock Isolator framework mocks overloaded methods only for objects of the type mocked.
For example, there is a type Base with a method foo and a Derived class. When mocking Derived, the foo method will be mocked ONLY for objects that are Derived. Calls to foo from objects that are Base will not be mocked. In other words, the Base.foo method is mocked only for the Derived class.

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. In other words, the Base.foo method is mocked only for the Base class.


I just wanted to make sure that I wasn't misinterpreting anything.
Note that if I don't overload the base method and instead I rename the 'Save' method in the child class, then I can mock the base method.

Thanks for taking a look.

- William
asked by wboatin (680 points)

3 Answers

0 votes
Hi William,

I'm a bit confused so maybe you can expand on that:
This test passes when run, when I really want the exception to be thrown and the test fail (just for this example)
What I want to happen is that when the overloaded method calls mybase.save, the save of the mustinherit class is mocked, and not the save on the child class.


So I'm guessing the second part is correct, and you want to mock the Save that throws the exception. Typemock Isolator has some limitations with overloads, but you can use Reflective mocks to do that:

    <TestMethod(), VerifyMocks()> _
    Public Sub MessageTester()

        Dim mock = MockManager.MockObject(Of Message)()
        mock.CallBase().ExpectCall("Save")
        Dim oMessage = mock.Object

        oMessage.Save("testing out mocking base class method", "I am about to throw an exception")
    End Sub


We're creating a Message object, and put the expectation on its base class. Then we call the Save method, which writes to the console the first parameter, but doesn't throw the exception.

Let me know if that's helpful, or if you need further assistance.
answered by gilz (14.5k points)
0 votes
I apologize for the lack of clarity in what i expected from my example.
I wanted the base class method 'Save' to be mocked, but it was the derived class method that was mocked.
However, your suggestion worked!
I had previously tried something like that, but missed the part about creating a variable of type Message and assigning to it the Mocked message.

Thanks!
answered by wboatin (680 points)
0 votes
I apologize for the lack of clarity in what i expected from my example.
I wanted the base class method 'Save' to be mocked, but it was the derived class method that was mocked.
However, your suggestion worked!
I had previously tried something like that, but missed the part about creating a variable of type Message and assigning to it the Mocked message.

Thanks!
answered by wboatin (680 points)
...