Hi,
It's a nice idea but, as your second email suggests, it does not work as you are hoping.
The `new` keyword in C# makes a new method of the same name; which one will be called depends on what type reference you call it from.
Let's create a modified wrapper:
public ClassWrapper2 : Class1 {
public virtual new String Say() { return "wrapper"; }
}
ClassWrapper2 classWrapper = new ClassWrapper2();
Class1 class1Ref = classWrapper;
classWrapper.Say(); // Calls ClassWrapper2.Say => "wrapper'
class1Ref.Say(); //Calls Class1.Say => "I am Class1."
((Class1) classWrapper).Say(); //Calls Class1.Say => "I am Class1."
(This is all plain C#; no NSubstitute involved)
So when you mock the class wrapper and set a `Returns`, it does do that, but the real code calls it via `Class1 and so the non-virtual method is called and NSubstitute does not get invoked.
One way to deal with code you don't own like this is to create your own adapter for it. In this case:
public class MyWrapper {
Class1 dep;
public MyWrapper(Class1 dependency) {
dep = dependency;
}
public virtual string Say() { return dep.Say(); }
}
Now the real code can use the mockable MyWrapper instead of Class1.
Hope this helps.
Regards,
David