Skip to content

NUnit2021

Mikkel Nylander Bundgaard edited this page Apr 25, 2020 · 2 revisions

NUnit2021

Incompatible types for EqualTo constraint.

Topic Value
Id NUnit2021
Severity Warning
Enabled True
Category Assertion
Code EqualToIncompatibleTypesAnalyzer

Description

Provided actual and expected arguments cannot be equal, therefore assertion is invalid.

Motivation

class Foo { }
class Bar { }

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

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

There is no way that instances of types Foo and Bar could be considered equal, therefore such assertion will always fail.

How to fix violations

Fix your assertion (i.e. fix actual or expected value, or choose another constraint).

Configure severity

Via ruleset file.

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

Via #pragma directive.

#pragma warning disable NUnit2021 // Incompatible types for EqualTo constraint.
Code violating the rule here
#pragma warning restore NUnit2021 // Incompatible types for EqualTo constraint.

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

#pragma warning disable NUnit2021 // Incompatible types for EqualTo constraint.

Via attribute [SuppressMessage].

[System.Diagnostics.CodeAnalysis.SuppressMessage("Assertion", 
    "NUnit2021:Incompatible types for EqualTo constraint.",
    Justification = "Reason...")]
Clone this wiki locally