Skip to content

Commit

Permalink
Update MSTEST0038 rule to include Assert.AreNotSame
Browse files Browse the repository at this point in the history
  • Loading branch information
Youssef1313 authored Jan 7, 2025
1 parent 8593480 commit b622828
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions docs/core/testing/mstest-analyzers/mstest0038.md
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
Expand All @@ -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

View workflow job for this annotation

GitHub Actions / lint

Trailing spaces

docs/core/testing/mstest-analyzers/mstest0038.md:35:101 MD009/no-trailing-spaces Trailing spaces [Expected: 0 or 2; Actual: 1] https://github.com/DavidAnson/markdownlint/blob/v0.36.1/doc/md009.md

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

View workflow job for this annotation

GitHub Actions / lint

Files should end with a single newline character

docs/core/testing/mstest-analyzers/mstest0038.md:46:126 MD047/single-trailing-newline Files should end with a single newline character https://github.com/DavidAnson/markdownlint/blob/v0.36.1/doc/md047.md

0 comments on commit b622828

Please sign in to comment.