-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update MSTEST0038 rule to include
Assert.AreNotSame
- Loading branch information
1 parent
8593480
commit b622828
Showing
1 changed file
with
13 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
--- | ||
title: "Don't use 'Assert.AreSame' with value types" | ||
description: "Learn about code analysis rule MSTEST0038: Don't use 'Assert.AreSame' with value types" | ||
title: "Don't use 'Assert.AreSame' or 'Assert.AreNotSame' with value types" | ||
description: "Learn about code analysis rule MSTEST0038: Don't use 'Assert.AreSame' or 'Assert.AreNotSame' with value types" | ||
ms.date: 01/06/2025 | ||
f1_keywords: | ||
- MSTEST0038 | ||
|
@@ -11,33 +11,36 @@ helpviewer_keywords: | |
author: Youssef1313 | ||
ms.author: ygerges | ||
--- | ||
# MSTEST0038: Don't use 'Assert.AreSame' with value types | ||
# MSTEST0038: Don't use 'Assert.AreSame' or 'Assert.AreNotSame' with value types | ||
|
||
| Property | Value | | ||
|-------------------------------------|------------------------------------------------------------------------| | ||
| **Rule ID** | MSTEST0038 | | ||
| **Title** | Don't use 'Assert.AreSame' with value types | | ||
| **Title** | Don't use 'Assert.AreSame' or 'Assert.AreNotSame' with value types | | ||
| **Category** | Usage | | ||
| **Fix is breaking or non-breaking** | Non-breaking | | ||
| **Enabled by default** | Yes | | ||
| **Default severity** | Warning | | ||
| **Introduced in version** | 3.8.0 | | ||
| **Is there a code fix** | Yes | | ||
| **Is there a code fix** | No | | ||
|
||
## Cause | ||
|
||
The use of <xref:Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreSame%2A?displayProperty=nameWithType> with one or both arguments being a value type. | ||
The use of <xref:Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreSame%2A?displayProperty=nameWithType> or <xref:Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreNotSame%2A?displayProperty=nameWithType> with one or both arguments being a value type. | ||
|
||
## Rule description | ||
|
||
The way <xref:Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreSame%2A?displayProperty=nameWithType> works is by comparing the *reference* of the given expected and actual arguments via `ReferenceEquals`. Hence, when you pass a value type, it will be [boxed](../../../csharp/programming-guide/types/boxing-and-unboxing.md#boxing). So, the assert will always fail. | ||
The way <xref:Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreSame%2A?displayProperty=nameWithType> and <xref:Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreNotSame%2A?displayProperty=nameWithType> work is by comparing the *reference* of the given expected/notExpected and actual arguments via `ReferenceEquals`. Hence, when you pass a value type, it will be [boxed](../../../csharp/programming-guide/types/boxing-and-unboxing.md#boxing). | ||
|
||
The only case where this assert will pass is if both arguments are nullable value types whose values are both null. In this case, it's clearer to have two separate `Assert.IsNull` calls. | ||
If using `AreSame`, the assert will always fail. If using `AreNotSame`, the assert will always pass. | ||
Check failure on line 35 in docs/core/testing/mstest-analyzers/mstest0038.md GitHub Actions / lintTrailing spaces
|
||
|
||
The only case for `AreSame` when this assert will pass is if both arguments are nullable value types whose values are both null. In this case, it's clearer to have two separate `Assert.IsNull` calls. | ||
|
||
## How to fix violations | ||
|
||
If both arguments are nullable value types whose values are expected to be null, then use two separate `Assert.IsNull` calls. Otherwise, use `Assert.AreEqual` instead of `Assert.AreSame`. | ||
Use `Assert.AreEqual` and `Assert.AreNotEqual` instead of `Assert.AreSame` and `Assert.AreNotSame`. | ||
If using `Assert.AreSame` and both arguments are nullable value types whose values are expected to be null, then two separate `Assert.IsNull` calls may be a better fit than `AreEqual`, depending on the intent of the test. | ||
|
||
## When to suppress warnings | ||
|
||
Do not suppress a warning from this rule. Ignoring this rule will result in an assertion that will fail. | ||
Do not suppress a warning from this rule. Ignoring this rule will result in an assertion that will always fail or always pass. | ||
Check failure on line 46 in docs/core/testing/mstest-analyzers/mstest0038.md GitHub Actions / lintFiles should end with a single newline character
|