Back to Nunit

NUnit2044

docs/articles/nunit-analyzers/NUnit2044.md

latest1.8 KB
Original Source

NUnit2044

Non-delegate actual parameter

TopicValue
IdNUnit2044
SeverityError
EnabledTrue
CategoryAssertion
CodeDelegateRequiredAnalyzer

Description

The actual argument needs to be evaluated by the Assert to catch any exceptions.

Motivation

In order for the Assert.That to catch an exception or a timeout, the code must be a delegate so it can be evaluated by the method. If the parameter is not a delegate, it will be evaluated before the call to Assert.That and stop further execution.

How to fix violations

Convert the call into a delegate using a lambda expression or method group.

<!-- start generated config severity -->

Configure severity

Via ruleset file

Configure the severity per project, for more info see MSDN.

Via .editorconfig file

ini
# NUnit2044: Non-delegate actual parameter
dotnet_diagnostic.NUnit2044.severity = chosenSeverity

where chosenSeverity can be one of none, silent, suggestion, warning, or error.

Via #pragma directive

csharp
#pragma warning disable NUnit2044 // Non-delegate actual parameter
Code violating the rule here
#pragma warning restore NUnit2044 // Non-delegate actual parameter

Or put this at the top of the file to disable all instances.

csharp
#pragma warning disable NUnit2044 // Non-delegate actual parameter

Via attribute [SuppressMessage]

csharp
[System.Diagnostics.CodeAnalysis.SuppressMessage("Assertion",
    "NUnit2044:Non-delegate actual parameter",
    Justification = "Reason...")]
<!-- end generated config severity -->