From 2b8b7fb310a180151ad0ed346c1031be269b67e2 Mon Sep 17 00:00:00 2001 From: Tom Longhurst <30480171+thomhurst@users.noreply.github.com> Date: Mon, 12 Jan 2026 21:21:49 +0000 Subject: [PATCH] fix: correct xUnit Assert.Same/NotSame conversion method names The xUnit migration code fixer was generating IsSameReference and IsNotSameReference, but the actual TUnit assertion methods are IsSameReferenceAs and IsNotSameReferenceAs. This fixes compilation errors (CS1061) when migrating xUnit tests that use Assert.Same() or Assert.NotSame(). Fixes #4333 Co-Authored-By: Claude Opus 4.5 --- .../XUnitMigrationCodeFixProvider.cs | 4 +- .../XUnitMigrationAnalyzerTests.cs | 76 +++++++++++++++++++ 2 files changed, 78 insertions(+), 2 deletions(-) diff --git a/TUnit.Analyzers.CodeFixers/XUnitMigrationCodeFixProvider.cs b/TUnit.Analyzers.CodeFixers/XUnitMigrationCodeFixProvider.cs index f566d0a81f..10b115a12e 100644 --- a/TUnit.Analyzers.CodeFixers/XUnitMigrationCodeFixProvider.cs +++ b/TUnit.Analyzers.CodeFixers/XUnitMigrationCodeFixProvider.cs @@ -569,9 +569,9 @@ protected override bool IsFrameworkAssertionNamespace(string namespaceName) // Reference assertions "Same" when arguments.Count >= 2 => - CreateTUnitAssertion("IsSameReference", arguments[1].Expression, arguments[0]), + CreateTUnitAssertion("IsSameReferenceAs", arguments[1].Expression, arguments[0]), "NotSame" when arguments.Count >= 2 => - CreateTUnitAssertion("IsNotSameReference", arguments[1].Expression, arguments[0]), + CreateTUnitAssertion("IsNotSameReferenceAs", arguments[1].Expression, arguments[0]), // String/Collection contains "Contains" when arguments.Count >= 2 => diff --git a/TUnit.Analyzers.Tests/XUnitMigrationAnalyzerTests.cs b/TUnit.Analyzers.Tests/XUnitMigrationAnalyzerTests.cs index 265731dd9b..5267105e04 100644 --- a/TUnit.Analyzers.Tests/XUnitMigrationAnalyzerTests.cs +++ b/TUnit.Analyzers.Tests/XUnitMigrationAnalyzerTests.cs @@ -861,6 +861,82 @@ public async Task MyTest() ); } + [Test] + public async Task Assert_Same_Can_Be_Converted() + { + await CodeFixer + .VerifyCodeFixAsync( + """ + {|#0:using TUnit.Core; + + public class MyClass + { + [Fact] + public void MyTest() + { + var expected = new object(); + var actual = expected; + Assert.Same(expected, actual); + } + }|} + """, + Verifier.Diagnostic(Rules.XunitMigration).WithLocation(0), + """ + using TUnit.Core; + + public class MyClass + { + [Test] + public async Task MyTest() + { + var expected = new object(); + var actual = expected; + await Assert.That(actual).IsSameReferenceAs(expected); + } + } + """, + ConfigureXUnitTest + ); + } + + [Test] + public async Task Assert_NotSame_Can_Be_Converted() + { + await CodeFixer + .VerifyCodeFixAsync( + """ + {|#0:using TUnit.Core; + + public class MyClass + { + [Fact] + public void MyTest() + { + var obj1 = new object(); + var obj2 = new object(); + Assert.NotSame(obj1, obj2); + } + }|} + """, + Verifier.Diagnostic(Rules.XunitMigration).WithLocation(0), + """ + using TUnit.Core; + + public class MyClass + { + [Test] + public async Task MyTest() + { + var obj1 = new object(); + var obj2 = new object(); + await Assert.That(obj2).IsNotSameReferenceAs(obj1); + } + } + """, + ConfigureXUnitTest + ); + } + private static void ConfigureXUnitTest(Verifier.Test test) { var globalUsings = ("GlobalUsings.cs", SourceText.From("global using Xunit;"));