Skip to content

Allow chaining a callback method before the Returns call. #98

@haacked

Description

@haacked

Suppose I have the following interface.

public interface IDoStuffs
{
    int Calculate();
}

I love the current syntax for setting up a return value. So simple.

var stuffDoer = Substitute.For<IDoStuffs>();
stuffDoer.Calculate().Returns(42);

But if I need to also run a callback, my tight clean code gets a bit ugly. Not too ugly, but ugly. I can no longer use a simple lambda, I have to use a code block.

var manualResetEvent = new ManualResetEvent();
var stuffDoer = Substitute.For<IDoStuffs>();
stuffDoer.Calculate().Returns(x => {
    manualResetEvent.Signal();
    return 42;
});

What I'd love to do instead is chain a callback before the Returns call.

var manualResetEvent = new ManualResetEvent();
var stuffDoer = Substitute.For<IDoStuffs>();
stuffDoer.Calculate().Do(() => manualResetEvent.Signal()).Returns(42);

If it feels more natural, you could chain it to the end instead. But the behavior would probably be the same.

var manualResetEvent = new ManualResetEvent();
var stuffDoer = Substitute.For<IDoStuffs>();
stuffDoer.Calculate().Returns(42).Do(() => manualResetEvent.Signal());

Bonus if the callback method has 2-overloads:

  1. One that accepts a parameterless Action.
  2. One that accepts an Action with the same arguments as the method being substituted.

Thoughts?

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions