Skip to content

Latest commit

 

History

History
48 lines (35 loc) · 1.17 KB

File metadata and controls

48 lines (35 loc) · 1.17 KB

NS5003

CheckId NS5003
Category Usage

Cause

Sync Throws used in async method.

Rule description

A violation of this rule occurs when Throws or ThrowsForAnyArgs is used in combination with async method.

How to fix violations

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()));

How to suppress violations

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.