docs/articles/nunit-analyzers/NUnit2008.md
| Topic | Value |
|---|---|
| Id | NUnit2008 |
| Severity | Warning |
| Enabled | True |
| Category | Assertion |
| Code | IgnoreCaseUsageAnalyzer |
The IgnoreCase modifier should only be used for string or char arguments. Using it on another type will not have any effect.
To bring developers' attention to a scenario in which their code is actually having no effect and may reveal that their test is not doing what they expect.
[Test]
public void NUnit2008SampleTest()
{
var date = DateTime.Now;
Assert.That(date, Is.Not.EqualTo(date.AddDays(1)).IgnoreCase);
}
Using IgnoreCase here doesn't make any sense, because the types we're comparing don't have the concept of case.
Therefore, it's only suitable to use on textual primitives (e.g. string and char).
Remove the errant call to IgnoreCase:
[Test]
public void NUnit2008SampleTest()
{
var date = DateTime.Now;
Assert.That(date, Is.Not.EqualTo(date.AddDays(1)));
}
Or update the code to compare based on the primitives that are supported by IgnoreCase:
[Test]
public void NUnit2008SampleTest()
{
var date = DateTime.Now;
Assert.That(date.ToString(), Is.Not.EqualTo(date.AddDays(1).ToString()).IgnoreCase);
}
Configure the severity per project, for more info see MSDN.
# NUnit2008: Incorrect IgnoreCase usage
dotnet_diagnostic.NUnit2008.severity = chosenSeverity
where chosenSeverity can be one of none, silent, suggestion, warning, or error.
#pragma warning disable NUnit2008 // Incorrect IgnoreCase usage
Code violating the rule here
#pragma warning restore NUnit2008 // Incorrect IgnoreCase usage
Or put this at the top of the file to disable all instances.
#pragma warning disable NUnit2008 // Incorrect IgnoreCase usage
[SuppressMessage][System.Diagnostics.CodeAnalysis.SuppressMessage("Assertion",
"NUnit2008:Incorrect IgnoreCase usage",
Justification = "Reason...")]