Skip to content

Commit 8bc7388

Browse files
authored
Fixed IsNotInstanceOfType failing when objected being asserted on is null (#622)
* Update Assert.cs * Update Assert.cs * Update AssertTests.cs * Update Assert.cs * Update Assert.cs
1 parent 38bd996 commit 8bc7388

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

src/TestFramework/MSTest.Core/Assertions/Assert.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1754,11 +1754,17 @@ public static void IsNotInstanceOfType(object value, Type wrongType, string mess
17541754
/// </exception>
17551755
public static void IsNotInstanceOfType(object value, Type wrongType, string message, params object[] parameters)
17561756
{
1757-
if (wrongType == null || value == null)
1757+
if (wrongType == null)
17581758
{
17591759
HandleFail("Assert.IsNotInstanceOfType", message, parameters);
17601760
}
17611761

1762+
// Null is not an instance of any type.
1763+
if (value == null)
1764+
{
1765+
return;
1766+
}
1767+
17621768
var elementTypeInfo = value.GetType().GetTypeInfo();
17631769
var expectedTypeInfo = wrongType.GetTypeInfo();
17641770
if (expectedTypeInfo.IsAssignableFrom(elementTypeInfo))

test/UnitTests/MSTest.Core.Unit.Tests/Assertions/AssertTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,6 @@ public void InstanceOfTypeShouldPassOnHigherInstance()
278278
public void InstanceNotOfTypeShouldFailWhenValueIsNull()
279279
{
280280
Action action = () => TestFrameworkV2.Assert.IsNotInstanceOfType(null, typeof(AssertTests));
281-
ActionUtility.ActionShouldThrowExceptionOfType(action, typeof(TestFrameworkV2.AssertFailedException));
282281
}
283282

284283
[TestMethod]

0 commit comments

Comments
 (0)