|
| 1 | +# NUnit2054 |
| 2 | + |
| 3 | +<!-- markdownlint-disable-next-line MD013 --> |
| 4 | +## Consider using Assert.That(actual, Is.Not.AssignableFrom(expected)) instead of ClassicAssert.IsNotAssignableFrom(expected, actual) |
| 5 | + |
| 6 | +| Topic | Value |
| 7 | +| :-- | :-- |
| 8 | +| Id | NUnit2054 |
| 9 | +| Severity | Info |
| 10 | +| Enabled | True |
| 11 | +| Category | Assertion |
| 12 | +| Code | [ClassicModelAssertUsageAnalyzer](https://github.com/nunit/nunit.analyzers/blob/master/src/nunit.analyzers/ClassicModelAssertUsage/ClassicModelAssertUsageAnalyzer.cs) |
| 13 | + |
| 14 | +## Description |
| 15 | + |
| 16 | +Consider using the constraint model, `Assert.That(actual, Is.Not.AssignableFrom(expected))`, instead of the classic model, |
| 17 | +`ClassicAssert.IsNotAssignableFrom(expected, actual)`. |
| 18 | + |
| 19 | +## Motivation |
| 20 | + |
| 21 | +The assert `ClassicAssert.IsNotAssignableFrom` from the classic Assert model makes it easy to confuse the `expected` |
| 22 | +and the `actual` argument, so this analyzer marks usages of `ClassicAssert.IsNotAssignableFrom`. |
| 23 | + |
| 24 | +```csharp |
| 25 | +[Test] |
| 26 | +public void Test() |
| 27 | +{ |
| 28 | + ClassicAssert.IsNotAssignableFrom(expected, actual); |
| 29 | +} |
| 30 | +``` |
| 31 | + |
| 32 | +## How to fix violations |
| 33 | + |
| 34 | +The analyzer comes with a code fix that will replace `ClassicAssert.IsNotAssignableFrom(expected, actual)` with |
| 35 | +`Assert.That(actual, Is.Not.AssignableFrom(expected))`. So the code block above will be changed into. |
| 36 | + |
| 37 | +```csharp |
| 38 | +[Test] |
| 39 | +public void Test() |
| 40 | +{ |
| 41 | + Assert.That(actual, Is.Not.AssignableFrom(expected)); |
| 42 | +} |
| 43 | +``` |
| 44 | + |
| 45 | +<!-- start generated config severity --> |
| 46 | +## Configure severity |
| 47 | + |
| 48 | +### Via ruleset file |
| 49 | + |
| 50 | +Configure the severity per project, for more info see |
| 51 | +[MSDN](https://learn.microsoft.com/en-us/visualstudio/code-quality/using-rule-sets-to-group-code-analysis-rules?view=vs-2022). |
| 52 | + |
| 53 | +### Via .editorconfig file |
| 54 | + |
| 55 | +```ini |
| 56 | +# NUnit2054: Consider using Assert.That(actual, Is.Not.AssignableFrom(expected)) instead of ClassicAssert.IsNotAssignableFrom(expected, actual) |
| 57 | +dotnet_diagnostic.NUnit2054.severity = chosenSeverity |
| 58 | +``` |
| 59 | + |
| 60 | +where `chosenSeverity` can be one of `none`, `silent`, `suggestion`, `warning`, or `error`. |
| 61 | + |
| 62 | +### Via #pragma directive |
| 63 | + |
| 64 | +```csharp |
| 65 | +#pragma warning disable NUnit2054 // Consider using Assert.That(actual, Is.Not.AssignableFrom(expected)) instead of ClassicAssert.IsNotAssignableFrom(expected, actual) |
| 66 | +Code violating the rule here |
| 67 | +#pragma warning restore NUnit2054 // Consider using Assert.That(actual, Is.Not.AssignableFrom(expected)) instead of ClassicAssert.IsNotAssignableFrom(expected, actual) |
| 68 | +``` |
| 69 | + |
| 70 | +Or put this at the top of the file to disable all instances. |
| 71 | + |
| 72 | +```csharp |
| 73 | +#pragma warning disable NUnit2054 // Consider using Assert.That(actual, Is.Not.AssignableFrom(expected)) instead of ClassicAssert.IsNotAssignableFrom(expected, actual) |
| 74 | +``` |
| 75 | + |
| 76 | +### Via attribute `[SuppressMessage]` |
| 77 | + |
| 78 | +```csharp |
| 79 | +[System.Diagnostics.CodeAnalysis.SuppressMessage("Assertion", |
| 80 | + "NUnit2054:Consider using Assert.That(actual, Is.Not.AssignableFrom(expected)) instead of ClassicAssert.IsNotAssignableFrom(expected, actual)", |
| 81 | + Justification = "Reason...")] |
| 82 | +``` |
| 83 | +<!-- end generated config severity --> |
0 commit comments