Looking at the source for the CallActions
, there doesn't appear to be a way to remove or replace a callback.
Using an example proving the lack of replace functionality
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));
where IFoo
is
public interface IFoo
{
void Bar();
}
Lacking a change to the NSubstitute API, a workaround is:
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));