-
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
[GH-134] - detecting async Received.InOrder callbacks #137
Conversation
b94d400
to
8645223
Compare
|
||
namespace NSubstitute.Analyzers.Shared.DiagnosticAnalyzers | ||
{ | ||
internal abstract class AbstractAsyncReceivedInOrderCallbackAnalyzer<TSyntaxKind, TInvocationExpressionSyntax> : AbstractDiagnosticAnalyzer |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dtchepak @zvirja this will detect async callback in following usages
Received.InOrder(async () => { // rest of the code })
Received.InOrder(async delegate { // rest of the code })
should I also make an attempt to detect async void callbacks like
Received.InOrder(Calls);
private async void Calls()
{
await Task.CompletedTask;
}
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe hold off for now. I haven't done much with async/await so I'm not sure, but maybe people will want to try asserting calls on a substitute that is returned from another async call:
private async void Calls() {
var anotherSub = await sub.GetThingoe();
anotherSub.SomeCall();
}
AFAICT the problem for NSubstitute is mainly when using Received.InOrder(async () => ...)
; it seems ok without the async
which I think is what we need to avoid. Keeping the option to extract this to a separate method might be a good workaround if people really need this. WDYT @zvirja ?
But I'm happy to be corrected on this as I only have basic experience with async/await.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dtchepak I like you idea of keeping Received.InOrder(Calls);
for now in case clients have weird reason for that. This way we could also collect the feedback and see whether it's the case at all.
Also it should help keep code simpler and less chance of false-positive :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your feedback guys. I am leaving this as it is
documentation/rules/NS5002.md
Outdated
|
||
## Rule description | ||
|
||
A violation of this rule occurs when async callback is used in `Received.InOrder` method. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should add a description of why this is a problem. Maybe just link to the issue?
A violation of this rule occurs when async callback is used in
Received.InOrder
method.Received.InOrder
is used to specify calls which should be received by one or more substitutes. Running/awaiting these calls is not required for this, and can cause some problems as described in issue #604.
A violation of this rule occurs when async callback is used in `Received.InOrder` method. `Received.InOrder` is used to specify calls which should be received by one or more substitutes. Running/awaiting these calls is not required for this, and can cause some problems as described in issue [#604](https://github.com/nsubstitute/NSubstitute/issues/604).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed as suggested
|
||
namespace NSubstitute.Analyzers.Shared.DiagnosticAnalyzers | ||
{ | ||
internal abstract class AbstractAsyncReceivedInOrderCallbackAnalyzer<TSyntaxKind, TInvocationExpressionSyntax> : AbstractDiagnosticAnalyzer |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe hold off for now. I haven't done much with async/await so I'm not sure, but maybe people will want to try asserting calls on a substitute that is returned from another async call:
private async void Calls() {
var anotherSub = await sub.GetThingoe();
anotherSub.SomeCall();
}
AFAICT the problem for NSubstitute is mainly when using Received.InOrder(async () => ...)
; it seems ok without the async
which I think is what we need to avoid. Keeping the option to extract this to a separate method might be a good workaround if people really need this. WDYT @zvirja ?
But I'm happy to be corrected on this as I only have basic experience with async/await.
8645223
to
eb440e9
Compare
Closes #134