Your question is very important, but if you think about it, there is no other way to mock the class. If you take the interface, itโs just a contract, so the mock-up framework can make fun of the way you want, but if you take the class, it already has an implementation for it.
Thus, in order to be able to make fun of class members, the mock framework must inherit from the class and redefine the behavior of the member in accordance with the request, and only virtual members will work for this purpose.
For example, if you have (I show methods, but the same is true for properties)
class Foo { public void Bar() { } public virtual void virtualBar() { } }
then the breadboard framework probably creates something like this to mock
public class MockFoo : Foo { public override void virtualBar() {
Now that you have
Foo foo = GetMockObject();
now when you call
foo.Bar();
you are not going to name its actual implementation, but since it is not a virtual member, it is called Foo Bar()
on the other hand causing
foo.VirtualBar();
will call MockFoo VirtualBar() as it is a virtual member that will have the behavior introduced by the mock infrastructure upon request.
source share