Back to Nunit

NUnit2041

docs/articles/nunit-analyzers/NUnit2041.md

latest1.9 KB
Original Source

NUnit2041

Incompatible types for comparison constraint

TopicValue
IdNUnit2041
SeverityError
EnabledTrue
CategoryAssertion
CodeComparableTypesAnalyzer

Description

The comparison constraint always fails as the actual and the expected value are not comparable.

Motivation

csharp
class Foo { }
class Bar { }

var foo = new Foo();
var bar = new Bar();

Assert.That(foo, Is.GreaterThan(bar));

There is no comparisons defined between instances of types Foo and Bar, therefore such assertion will always fail.

How to fix violations

Fix your assertion (i.e. fix actual or expected value) or implement IComparable<Bar> on Foo.

<!-- start generated config severity -->

Configure severity

Via ruleset file

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

Via .editorconfig file

ini
# NUnit2041: Incompatible types for comparison constraint
dotnet_diagnostic.NUnit2041.severity = chosenSeverity

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

Via #pragma directive

csharp
#pragma warning disable NUnit2041 // Incompatible types for comparison constraint
Code violating the rule here
#pragma warning restore NUnit2041 // Incompatible types for comparison constraint

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

csharp
#pragma warning disable NUnit2041 // Incompatible types for comparison constraint

Via attribute [SuppressMessage]

csharp
[System.Diagnostics.CodeAnalysis.SuppressMessage("Assertion",
    "NUnit2041:Incompatible types for comparison constraint",
    Justification = "Reason...")]
<!-- end generated config severity -->