I’ve been doing some mocking with RhinoMocks and it requires that mocked methods be made virtual. This is fine except we have a custom framework which contains the methods that I want to mock which are currently not marked as virtual.
I can’t forsee any problem with making these methods virtual but I was wondering what are some potential dangers of making methods virtual that I should look out for?
Actually it can be very problematic if the method is not designed to be overridden and someone overrides it. In particular, never call a virtual method from a constructor. Consider:
The Derived class may not be aware that the virtual method call will result in its InitializeComponent method being called before a single line of its own constructor has run.