Skip to content

NUnit2015

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

NUnit2015

Consider using Assert.That(expr2, Is.SameAs(expr1)) instead of Assert.AreSame(expr1, expr2).

Topic Value
Id NUnit2015
Severity Warning
Enabled True
Category Assertion
Code ClassicModelAssertUsageAnalyzer

Description

Consider using the constraint model, Assert.That(expr2, Is.SameAs(expr1)), instead of the classic model, Assert.AreSame(expr1, expr2).

Motivation

The assert Assert.AreSame from the classic Assert model makes it easy to confuse the expected and the actual argument, so this analyzer marks usages of Assert.AreSame.

[Test]
public void Test()
{
    Assert.AreSame(expected, actual);
}

How to fix violations

The analyzer comes with a code fix that will replace Assert.AreSame(expected, actual) with Assert.That(actual, Is.SameAs(expected)). So the code block above will be changed into.

[Test]
public void Test()
{
    Assert.That(actual, Is.SameAs(expected));
}

Configure severity

Via ruleset file.

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

Via #pragma directive.

#pragma warning disable NUnit2015 // Consider using Assert.That(expr2, Is.SameAs(expr1)) instead of Assert.AreSame(expr1, expr2).
Code violating the rule here
#pragma warning restore NUnit2015 // Consider using Assert.That(expr2, Is.SameAs(expr1)) instead of Assert.AreSame(expr1, expr2).

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

#pragma warning disable NUnit2015 // Consider using Assert.That(expr2, Is.SameAs(expr1)) instead of Assert.AreSame(expr1, expr2).

Via attribute [SuppressMessage].

[System.Diagnostics.CodeAnalysis.SuppressMessage("Assertion", 
    "NUnit2015:Consider using Assert.That(expr2, Is.SameAs(expr1)) instead of Assert.AreSame(expr1, expr2).",
    Justification = "Reason...")]
Clone this wiki locally