查看的来源,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));