diff --git a/src/TestFramework/TestFramework/Assertions/Assert.IsExactInstanceOfType.cs b/src/TestFramework/TestFramework/Assertions/Assert.IsExactInstanceOfType.cs index 144f17cdbe..67b713c393 100644 --- a/src/TestFramework/TestFramework/Assertions/Assert.IsExactInstanceOfType.cs +++ b/src/TestFramework/TestFramework/Assertions/Assert.IsExactInstanceOfType.cs @@ -341,6 +341,23 @@ private static void ThrowAssertIsExactInstanceOfTypeFailed(object? value, Type? expectedType.ToString(), value.GetType().ToString()); } + else if (value is null) + { + // Ensure proper punctuation before adding the NULL message + string trimmedMessage = userMessage.TrimEnd(); + if (trimmedMessage.Length > 0 && (trimmedMessage[^1] == '.' || trimmedMessage[^1] == '!' || trimmedMessage[^1] == '?')) + { + finalMessage = $"{trimmedMessage} 'value' was NULL."; + } + else if (trimmedMessage.Length > 0) + { + finalMessage = $"{trimmedMessage}. 'value' was NULL."; + } + else + { + finalMessage = "'value' was NULL."; + } + } ThrowAssertFailed("Assert.IsExactInstanceOfType", finalMessage); } diff --git a/test/UnitTests/TestFramework.UnitTests/Assertions/AssertTests.IsExactInstanceOfTypeTests.cs b/test/UnitTests/TestFramework.UnitTests/Assertions/AssertTests.IsExactInstanceOfTypeTests.cs index af72c579fa..c33356a318 100644 --- a/test/UnitTests/TestFramework.UnitTests/Assertions/AssertTests.IsExactInstanceOfTypeTests.cs +++ b/test/UnitTests/TestFramework.UnitTests/Assertions/AssertTests.IsExactInstanceOfTypeTests.cs @@ -12,7 +12,7 @@ public void ExactInstanceOfTypeShouldFailWhenValueIsNull() { Action action = () => Assert.IsExactInstanceOfType(null, typeof(AssertTests)); action.Should().Throw() - .WithMessage("Assert.IsExactInstanceOfType failed. 'value' expression: 'null'."); + .WithMessage("Assert.IsExactInstanceOfType failed. 'value' expression: 'null'. 'value' was NULL."); } public void ExactInstanceOfTypeShouldFailWhenTypeIsNull() @@ -56,7 +56,7 @@ public void ExactInstanceOfType_WithStringMessage_ShouldFailWhenValueIsNull() { Action action = () => Assert.IsExactInstanceOfType(null, typeof(AssertTests), "User-provided message"); action.Should().Throw() - .WithMessage("Assert.IsExactInstanceOfType failed. 'value' expression: 'null'. User-provided message"); + .WithMessage("Assert.IsExactInstanceOfType failed. 'value' expression: 'null'. User-provided message. 'value' was NULL."); } public void ExactInstanceOfType_WithStringMessage_ShouldFailWhenTypeIsNull() @@ -82,7 +82,7 @@ public async Task ExactInstanceOfType_WithInterpolatedString_ShouldFailWhenValue DateTime dateTime = DateTime.Now; Func action = async () => Assert.IsExactInstanceOfType(null, typeof(AssertTests), $"User-provided message. {o}, {o,35}, {await GetHelloStringAsync()}, {new DummyIFormattable()}, {dateTime:tt}, {dateTime,5:tt}"); (await action.Should().ThrowAsync()) - .WithMessage($"Assert.IsExactInstanceOfType failed. 'value' expression: 'null'. User-provided message. DummyClassTrackingToStringCalls, DummyClassTrackingToStringCalls, Hello, DummyIFormattable.ToString(), {string.Format(null, "{0:tt}", dateTime)}, {string.Format(null, "{0,5:tt}", dateTime)}"); + .WithMessage($"Assert.IsExactInstanceOfType failed. 'value' expression: 'null'. User-provided message. DummyClassTrackingToStringCalls, DummyClassTrackingToStringCalls, Hello, DummyIFormattable.ToString(), {string.Format(null, "{0:tt}", dateTime)}, {string.Format(null, "{0,5:tt}", dateTime)}. 'value' was NULL."); o.WasToStringCalled.Should().BeTrue(); } @@ -141,7 +141,7 @@ public void IsExactInstanceOfTypeUsingGenericType_WhenValueIsNull_Fails() { Action action = () => Assert.IsExactInstanceOfType(null); action.Should().Throw() - .WithMessage("Assert.IsExactInstanceOfType failed. 'value' expression: 'null'."); + .WithMessage("Assert.IsExactInstanceOfType failed. 'value' expression: 'null'. 'value' was NULL."); } public void IsExactInstanceOfTypeUsingGenericType_WhenTypeMismatch_Fails()