NSubstitute当...定义没有被后来的定义覆盖 [英] NSubstitute When...Do definition is not getting overriden by subsequent definitions

查看:198
本文介绍了NSubstitute当...定义没有被后来的定义覆盖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  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
}

对于上面的代码片段,(1)和(2)都显示counter ++行为,表明When ... Do行为没有被覆盖。我需要这种行为生成我想连接不同回调的测试场景。



我应该如何实现这个?

c> c> c>回调不会被替换,但都应该执行。例如(使用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

建议When..Dom谨慎使用,因为它的使用可能是失败封装的症状。将行为强制为替换对象可以表明我们正在测试的类与依赖类的行为深度耦合,而不是我们替代的接口。



这个免责声明,你可以交换回调的一种方法是使用一个帮助类来提供特定的回调:

  [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 =>计数器 - ;
sub.Bar();
Console.WriteLine(counter);

helper.Callback = x => {counter =(counter + 1)* 10; };
sub.Bar();
Console.WriteLine(counter);
}

public class CallbackHelper {
public Action< CallInfo>回电话;
}

/ *列印:
1
0
10
* /

如果发布了行为交换的具体示例,您试图实现,我们可能能够提出一个接口更改,以避免使用



希望这有助于。 :)


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 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. :)

这篇关于NSubstitute当...定义没有被后来的定义覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆