void ABC()
{
var foo = Substitute.For<IFoo>();
foo.When(x => x.Bar()).Do(x => counter++);
<use Bar()>.... 1
foo.When(x => x.Bar()).Do(x => counter--);
<use Bar()>.... 2
}
For the above code snippet both (1) and (2) are displaying the counter++ behavior indicating that the When…Do behavior is not getting overridden. I need this behavior for generating my testing scenario where I want to hookup different callbacks.
How should I achieve this?
The
Docallback does not get replaced, but both should execute. For example (using NSub 1.4.3.0):I suggest that When..Do be used sparingly, as its use can be a symptom of failed encapsulation. Forcing behaviour into a substituted object can indicate that the class we are testing has deep coupling to the behaviour of a dependent class, rather than the interface we are substituting for.
With that disclaimer, one way you can swap out callbacks is to use a helper class to supply the specific callback:
If you post the specific example of behaviour-swapping you are trying to achieve, we may be able to come up with an interface change to avoid the use of this altogether.
Hope this helps. 🙂