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