问题
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?
回答1:
The Do
callback does not get replaced, but both should execute. For example (using NSub 1.4.3.0):
var counter = 0;
var sub = Substitute.For<IFoo>();
sub.When(x => x.Bar()).Do(x => counter++);
sub.Bar();
Console.WriteLine(counter); // prints 1
sub.When(x => x.Bar()).Do(x => counter--);
sub.Bar();
Console.WriteLine(counter); // prints 1, as counter gets inc'd to 2, then dec'd to 1
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:
[Test]
public void Example() {
var counter = 0;
var helper = new CallbackHelper();
helper.Callback = x => counter++;
var sub = Substitute.For<IFoo>();
sub.When(x => x.Bar()).Do(x => helper.Callback(x));
sub.Bar();
Console.WriteLine(counter);
helper.Callback = x => counter--;
sub.Bar();
Console.WriteLine(counter);
helper.Callback = x => { counter = (counter+1) * 10; };
sub.Bar();
Console.WriteLine(counter);
}
public class CallbackHelper {
public Action<CallInfo> Callback;
}
/* Prints:
1
0
10
*/
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. :)
来源:https://stackoverflow.com/questions/14273446/nsubstitute-when-do-definition-is-not-getting-overriden-by-subsequent-definiti