Consider using Assert.That(actual, Is.Not.SameAs(expected)) instead of Assert.AreNotSame(expected, actual)
Topic | Value |
---|---|
Id | NUnit2031 |
Severity | Warning |
Enabled | True |
Category | Assertion |
Code | ClassicModelAssertUsageAnalyzer |
Consider using the constraint model, Assert.That(actual, Is.Not.SameAs(expected))
, instead of the classic model, Assert.AreNotSame(expected, actual)
.
The assert Assert.AreNotSame
from the classic Assert model makes it easy to confuse the expected
and the actual
argument,
so this analyzer marks usages of Assert.AreNotSame
.
[Test]
public void Test()
{
Assert.AreNotSame(expected, actual);
}
The analyzer comes with a code fix that will replace Assert.AreNotSame(expected, actual)
with
Assert.That(actual, Is.Not.SameAs(expected))
. So the code block above will be changed into.
[Test]
public void Test()
{
Assert.That(actual, Is.Not.SameAs(expected));
}
Configure the severity per project, for more info see MSDN.
# NUnit2031: Consider using Assert.That(actual, Is.Not.SameAs(expected)) instead of Assert.AreNotSame(expected, actual)
dotnet_diagnostic.NUnit2031.severity = chosenSeverity
where chosenSeverity
can be one of none
, silent
, suggestion
, warning
, or error
.
#pragma warning disable NUnit2031 // Consider using Assert.That(actual, Is.Not.SameAs(expected)) instead of Assert.AreNotSame(expected, actual)
Code violating the rule here
#pragma warning restore NUnit2031 // Consider using Assert.That(actual, Is.Not.SameAs(expected)) instead of Assert.AreNotSame(expected, actual)
Or put this at the top of the file to disable all instances.
#pragma warning disable NUnit2031 // Consider using Assert.That(actual, Is.Not.SameAs(expected)) instead of Assert.AreNotSame(expected, actual)
[System.Diagnostics.CodeAnalysis.SuppressMessage("Assertion",
"NUnit2031:Consider using Assert.That(actual, Is.Not.SameAs(expected)) instead of Assert.AreNotSame(expected, actual)",
Justification = "Reason...")]