I've got this code:
sub.Foo(Arg.Do<int>(() => {}));
How do I undo this?
.ClearReceivedCalls()
and .ClearReturnValues()
seem to have no effect on removing this delegate
I've got this code:
sub.Foo(Arg.Do<int>(() => {}));
How do I undo this?
.ClearReceivedCalls()
and .ClearReturnValues()
seem to have no effect on removing this delegate
There isn't currently a way to remove the action, but you can queue it up in different ways that will allow you to stop it running.
The first option is to use an external action, then change that at a later point in time.
// Clear action manually
Action<string> action = x => SideEffect();
sub.Foo(Arg.Do<string>(x => action(x)));
// ...
action = x => { };
A variant of that approach can let you modify the action to automatically clear itself (alternatively we could use a guard clause to make sure it only runs the required amount of times):
// Self-clearing action (run once):
Action<string> action = x =>
{
SideEffect();
action = _ => { };
};
sub.Foo(Arg.Do<string>(x => action(x)));
Another option is to use the Callback builder. This is works with When..Do
rather than Arg.Do
, but does give you a bit of control over how different sequences of actions can be queued up (see the documentation for a more involved example):
// Callback builder:
// (See )
sub.When(x => x.Foo(Arg.Any<string>()))
.Do(Callback.First(call => SideEffect()));