NUnit2040
Non-reference types for SameAs constraint
Topic | Value |
---|---|
Id | NUnit2040 |
Severity | Error |
Enabled | True |
Category | Assertion |
Code | SameAsOnValueTypesAnalyzer |
Description
The SameAs constraint always fails on value types as the actual and the expected value cannot be the same reference.
Motivation
var expected = Guid.Empty;
var actual = expected;
Assert.That(actual, Is.SameAs(expected));
As Guid
is a struct
, actual will be a copy of expected but not have the same reference.
How to fix violations
Replace Is.SameAs
with Is.EqualTo
.
var expected = Guid.Empty;
var actual = expected;
Assert.That(actual, Is.EqualTo(expected));
Configure severity
Via ruleset file
Configure the severity per project, for more info see MSDN.
Via .editorconfig file
# NUnit2040: Non-reference types for SameAs constraint
dotnet_diagnostic.NUnit2040.severity = chosenSeverity
where chosenSeverity
can be one of none
, silent
, suggestion
, warning
, or error
.
Via #pragma directive
#pragma warning disable NUnit2040 // Non-reference types for SameAs constraint
Code violating the rule here
#pragma warning restore NUnit2040 // Non-reference types for SameAs constraint
Or put this at the top of the file to disable all instances.
#pragma warning disable NUnit2040 // Non-reference types for SameAs constraint
Via attribute [SuppressMessage]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Assertion",
"NUnit2040:Non-reference types for SameAs constraint",
Justification = "Reason...")]