CheckId | NS5003 |
Category | Usage |
Sync Throws
used in async method.
A violation of this rule occurs when Throws
or ThrowsForAnyArgs
is used in combination with async method.
To fix a violation of this rule, replace Throws<Exception>
with Returns(Task.FromException(new Exception()))
For example:
// Incorrect:
sub.BarAsync().Throws<Exception>();
// Correct:
sub.BarAsync().Returns(Task.FromException(new Exception()));
This warning can be suppressed by disabling the warning in the ruleset file for the project. The warning can also be suppressed programmatically for an assembly:
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "NS5003:Synchronous exception thrown from async method.", Justification = "Reviewed")]
Or for a specific code block:
#pragma warning disable NS5003 // Synchronous exception thrown from async method.
// the code which produces warning
#pragma warning restore NS5003 // Synchronous exception thrown from async method.