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