Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document MSTEST0038: Don't use 'Assert.AreSame' with value types #44157

Merged
merged 7 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions docs/core/testing/mstest-analyzers/mstest0038.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
---
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
- AvoidAssertAreSameWithValueTypesAnalyzer
helpviewer_keywords:
- AvoidAssertAreSameWithValueTypesAnalyzer
- MSTEST0038
author: Youssef1313
ms.author: ygerges
---
# MSTEST0038: Don't use 'Assert.AreSame' or 'Assert.AreNotSame' with value types

| Property | Value |
|-------------------------------------|------------------------------------------------------------------------|
| **Rule ID** | MSTEST0038 |
| **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** | No |

## Cause

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.
Youssef1313 marked this conversation as resolved.
Show resolved Hide resolved

## Rule description

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).
Youssef1313 marked this conversation as resolved.
Show resolved Hide resolved

If using `AreSame`, the assert will always fail. If using `AreNotSame`, the assert will always pass.

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.
Youssef1313 marked this conversation as resolved.
Show resolved Hide resolved

## How to fix violations

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.
Youssef1313 marked this conversation as resolved.
Show resolved Hide resolved

## When to suppress warnings

Do not suppress a warning from this rule. Ignoring this rule will result in an assertion that will always fail or always pass.
1 change: 1 addition & 0 deletions docs/core/testing/mstest-analyzers/usage-rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ Identifier | Name | Description
[MSTEST0024](mstest0024.md) | DoNotStoreStaticTestContextAnalyzer | Do not store TestContext in a static member
[MSTEST0026](mstest0026.md) | AssertionArgsShouldAvoidConditionalAccessRuleId | Avoid conditional access in assertions
[MSTEST0037](mstest0037.md) | UseProperAssertMethodsAnalyzer | Use proper `Assert` methods
[MSTEST0038](mstest0038.md) | AvoidAssertAreSameWithValueTypesAnalyzer | Don't use `Assert.AreSame` or `Assert.AreNotSame` with value types
Loading