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

Provide more intuitive methods #782

Open
BenjaminAbt opened this issue Feb 27, 2024 · 3 comments
Open

Provide more intuitive methods #782

BenjaminAbt opened this issue Feb 27, 2024 · 3 comments

Comments

@BenjaminAbt
Copy link

Is your feature request related to a problem? Please describe.

NSubstitute is a great library and is one the preferred test tool for mocks, not least because of the Moq Drama. However, some situations require code that are not intuitive and are comparatively inconvenient to implement. On the one hand, you can read this in many blogs that present migration docs from Moq to NSubstitute, but I also get this as feedback myself when I show different test libraries in training courses. So it is a real disadvantage in the everyday life of NSubstitute. I'm noticing it myself, as the number of requests for NSubstitute coaching / support is currently rising.

For example, converting Times.AtLeast() from Moq to NSubstitute is very inconvenient and not sustainable. There is the topic Received range of calls #558, in which there is a workaround with Within.

In addition, the statement

Personally, I'd prefer Quantity.Within(min, maxInclusive) more, as builder pattern is too verbose to me. But both ways are good, so it doesn't matter too much.

Personal impressions are perfectly okay, and it would also be okay if several methods ultimately provide the same or similar tests.
The easier tests can be written, the better our .NET ecosystem will be.

Describe the solution you'd like

It would be great if many things were easier to implement with NSubstitute and people no longer had to google workarounds. That would make it easier to accept and move from Moq to NSubstitute.
A small example for fixing AtLeast - a specific case for my customers who are annoyed by this - can be implemented with just a few lines of code.
main...BenjaminAbt:NSubstitute:feature/more-methods

Like my customers, we are currently helping ourselves with extensions such as

public static class ExtendedQuantity
{
    public static Quantity Min(int value)
         => Quantity.Within(value, int.MaxValue);
}

but it would be better if there were sustainable, convenient and intuitive things directly in NSubstitute.

Thanks ♥

@304NotModified
Copy link
Contributor

I like the idea here, but IMO this GitHub issue is very broad and therefore hard to fix.

people no longer had to google workarounds.

I don't know what this exactly means, other than the given examples

@dtchepak
Copy link
Member

Hi @BenjaminAbt ,

Sorry for the delayed response.
If you have the time would you be happy to send your branch as a PR?

A few notes on the existing commit:

  • I'd prefer to avoid ReceiveAtLeast. We already dump so much into intellisense with extension methods on Object and I'd prefer not to exacerbate the problem. 😅
  • I think we only call Quantity.Matches with a List, so Count() would probably end up being more efficient than something like .Take(minInclusive).Count() for this case. Any thoughts on whether we should change Matches to take a list to make this more obvious?
  • Did you want to add any of AtMost, LessThan, MoreThan as well?
  • Existing problem, but if you do send through a PR would you also be able to add code docs to the public members of Quantity that are currently missing doc comments?

As per @304NotModified's reply, if you have other specific examples please create an issue/s for them. 👍

@BenjaminAbt
Copy link
Author

I like the idea here, but IMO this GitHub issue is very broad and therefore hard to fix.

Yes, that was the intention. I wanted to find out what the general attitude was before I put more time into it; also because there have been statements about it in the past (see above).

If you have the time would you be happy to send your branch as a PR?

For sure.

I'd prefer to avoid ReceiveAtLeast. We already dump so much into intellisense with extension methods on Object and I'd prefer not to exacerbate the problem.

Yes, I understand this, but I see devs struggling. IntelliSense is just normal in our .NET / Visual Studio ecosystem, and I just see developers browsing IntellISense instead of people looking at the docs. It's not a one-off thing either, I see it consistently.

Did you want to add any of AtMost, LessThan, MoreThan as well?

Sounds good.

Existing problem, but if you do send through a PR would you also be able to add code docs to the public members of Quantity that are currently missing doc comments?

Yes.

I don't know what this exactly means, other than the given examples

There is one thing I do differently now and recommend to other people: use CHatGPT instead of GitHub Copilot to migrate from Moq to NSubstite.

The results are better and people need less help with things they can't find the equivalent for.
GitHub Copilot has sometimes suggested things for NSubstite that correspond to Moq's API - of course that doesn't work because they dont exist.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants