Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow callbacks (Do/Does) before Returns #833

Open
Jason31569 opened this issue Oct 4, 2024 · 3 comments
Open

Allow callbacks (Do/Does) before Returns #833

Jason31569 opened this issue Oct 4, 2024 · 3 comments
Labels
feature-request Request for a new NSubstitute feature

Comments

@Jason31569
Copy link

Jason31569 commented Oct 4, 2024

Referencing issue #98...

While doing something like stuffDoer.Calculate().Returns(42).AndCalls(x => blah()); works, it is would make a lot more sense to be able to do something like stuffDoer.Calculate(something).Do(x => { }).Returns(42); instead

Take the below example

var items = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

var a = items.Where(x => x % 2 == 0).Skip(3).ToArray();
var b = items.Skip(3).Where(x => x % 2 == 0).ToArray();

Because execution is from left to right a=[8, 10], and b=[4, 6, 8, 10]

We are used to reading (and even expecting) statements to work from left to right. Someone new to NSubstitute (like myself) could find this confusing, and would not expect the trailing Do to run before the Returns

@dtchepak
Copy link
Member

Agree it reads better, but the trade-off is that we're adding yet another extension method on object (bloats intellisense/autocomplete). We could potentially have in NSubstitute.Extensions so people could opt-in 🤔 .

Another option is to use the existing .Returns overload with a call back to "do and return".

@304NotModified 304NotModified added the feature-request Request for a new NSubstitute feature label Oct 28, 2024
@Jason31569
Copy link
Author

Agree it reads better, but the trade-off is that we're adding yet another extension method on object (bloats intellisense/autocomplete). We could potentially have in NSubstitute.Extensions so people could opt-in 🤔 .

Maybe the NSubstitute.Extensions option is the way to go

Another option is to use the existing .Returns overload with a call back to "do and return".

Are you talking about one of these methods?

ConfiguredCall Returns<T>(this T value, Func<CallInfo, T> returnThis, params Func<CallInfo, T>[] returnThese)

ConfiguredCall Returns<T>(this Task<T> value, Func<CallInfo, T> returnThis, params Func<CallInfo, T>[] returnThese)

ConfiguredCall Returns<T>(this ValueTask<T> value, Func<CallInfo, T> returnThis, params Func<CallInfo, T>[] returnThese)

@dtchepak
Copy link
Member

Yes those Returns overloads take Funcs which let us do then return.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request Request for a new NSubstitute feature
Projects
None yet
Development

No branches or pull requests

3 participants