Skip to content

NUnit2020

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

NUnit2020

Incompatible types for SameAs constraint.

Topic Value
Id NUnit2020
Severity Warning
Enabled True
Category Assertion
Code SameAsIncompatibleTypesAnalyzer

Description

Provided actual and expected arguments cannot have same type, therefore SameAs assertion will always fail.

Motivation

class Foo { }
class Bar { }

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

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

There is no way that the same instance can be of type Foo and type Bar, 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 NUnit2020 // Incompatible types for SameAs constraint.
Code violating the rule here
#pragma warning restore NUnit2020 // Incompatible types for SameAs constraint.

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

#pragma warning disable NUnit2020 // Incompatible types for SameAs constraint.

Via attribute [SuppressMessage].

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