NSubstitute When…Do definition is not getting overriden by subsequent definitions

女生的网名这么多〃 提交于 2019-12-11 04:09:29

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!