-
Notifications
You must be signed in to change notification settings - Fork 7
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
Detect Arg matcher misuse #35
Comments
Hi @dtchepak |
@tpodolak I think
As an aside, I think it is also an error to use @alexandrnikitin @zvirja Can you think of other cases I've missed here? |
@dtchepak thanks for the clarification. I think this one will be tricky to implement, the approaches which I was using so far, will probably report false positives. I will take a deeper look at some alternative approaches |
Thanks @tpodolak. If it is not practical to implement then please close the issue or tag as |
@dtchepak as for today I am able to detect incorrect usages of Arg matcher, however, I am still trying to make the analyzer smart enough not to report false positives. For instance
For this piece of code, no diagnostics will be reported (correct behaviour). However this one
will be considered as invalid usage - even though it is valid. Similary for usages of actual methods (not lambdas or delegates) in Returns/Throws/When etc |
@tpodolak Sounds very promising! So it is possible? |
I think/hope so. It requires sort of wider analysis(I basically need to know if given function which uses Arg in the body, is used somewhere in Returns/When/Received statement). I am experimenting with approach presented here. Maybe it will be enough for our needs |
This would be great, because these are the most insidious errors. |
@dtchepak I am getting closer to finish this one (at least for some beta/alpha release) should we reconsider changing the title and description of diagnostic? You proposed
However, as you mentioned in #35 (comment) it is also possible to use arg matchers in Received.InOrder When, WhenForAnyArgs, DidNotReceive etc. Should we change the diagnostic message somehow to indicate the all possible usages? |
@tpodolak Yes I think so. Proposed description (take 2):
What do you think? I'm not sure about title. Here are a few ideas:
|
Update after discussion at [1] [1]: nsubstitute/NSubstitute.Analyzers#35 (comment)
@tpodolak I've got a PR in for the docs: nsubstitute/NSubstitute#549 |
Update after discussion at [1] [1]: nsubstitute/NSubstitute.Analyzers#35 (comment)
Update after discussion at [issue 35]. [issue 35]: nsubstitute/NSubstitute.Analyzers#35 (comment)
Update after discussion at [issue 35](nsubstitute/NSubstitute.Analyzers#35 (comment)).
Update after discussion at [issue-35]. [issue-35]: nsubstitute/NSubstitute.Analyzers#35 (comment)
Update after discussion at issue 35[^1]. [1]: nsubstitute/NSubstitute.Analyzers#35 (comment)
Update after discussion at issue 35[^1]. [^1]: nsubstitute/NSubstitute.Analyzers#35 (comment)
Update after discussion at issue 35[^1]. [^1]: nsubstitute/NSubstitute.Analyzers#35 (comment)
Update after discussion at issue 35[^1]. [^1]: nsubstitute/NSubstitute.Analyzers#35 (comment)
Update after discussion at issue 35[^1]. [^1]: nsubstitute/NSubstitute.Analyzers#35 (comment)
Update after discussion at issue 35[^1]. [^1]: nsubstitute/NSubstitute.Analyzers#35 (comment)
Update after discussion at issue 35[^1]. [^1]: nsubstitute/NSubstitute.Analyzers#35 (comment)
Update after discussion at issue 35[^1]. [^1]: nsubstitute/NSubstitute.Analyzers#35 (comment)
Update after discussion at issue 35[^1]. [^1]: nsubstitute/NSubstitute.Analyzers#35 (comment)
Another nice repro example in nsubstitute/NSubstitute#587. @tpodolak I tried the build here: #35 (comment) |
* [GH-35] - NS1004 arg matcher analyzer
People often use
Arg.Any<int>()
in real calls (rather than.Returns
stub or.Received
assertion) to represent "I don't mind what input is used for this test".This case is show in the documentation. There is also an example in this post (search for "Another example").
This causes problems for NSubstitute because it tries to use these argument matchers the next time it gets a call (it will consider the next call to be an attempt to stub, rather than a real call), and it can cause confusing test behaviour or test errors.
Proposed title:
Proposed description (from docs):
The text was updated successfully, but these errors were encountered: