3

NSubstitute 是否可以清除或删除以前的 .When().Do() 配置?

substitute.When(s => s.Method(1)).Do(c => { /* do something */ });
// code

substitute.When(s => s.Method(1)).Clear(); // <-- is this possible?
substitute.When(s => s.Method(1)).Do(c => { /* do something different */ });
// other code
4

1 回答 1

6

查看的来源CallActions似乎没有办法删除或替换回调。

使用示例证明缺少替换功能

int state = 0;
var substitute = Substitute.For<IFoo>();

substitute.When(s => s.Bar()).Do(c => state++);
substitute.Bar();
Assert.That(state, Is.EqualTo(1));

substitute.When(s => s.Bar()).Do(c => state--);
substitute.Bar();

// FAIL: Both Do delegates are executed and state == 1
Assert.That(state, Is.EqualTo(0));

IFoo在哪里

public interface IFoo
{
    void Bar();
}

缺少对 NSubstitute API 的更改,解决方法是:

var state = 0;
var substitute = Substitute.For<IFoo>();

Action<CallInfo>[] onBar = {c => state++};

substitute.When(s => s.Bar()).Do(c => onBar[0](c));
substitute.Bar();
Assert.That(state, Is.EqualTo(1));

onBar[0] = c => state--;
substitute.Bar();
Assert.That(state, Is.EqualTo(0));
于 2013-09-17T07:32:16.217 回答