Type assertions based on 'assignable from' are confusingly named
A violation of this rule occurs when using Assert.IsAssignableFrom
(or Assert.IsNotAssignableFrom
).
There is confusing about which arguments is “from” and when is “to” when using Assert.IsAssignableFrom
(and Assert.IsNotAssignableFrom
). We have added an overload of Assert.IsType
(and Assert.IsNotType
) that allows users to pass a flag whether they want strict (“exact”) type matching behavior (like Assert.IsType
without the flag) or compatible (“inexact”) type matching behavior (like Assert.IsAssignableFrom
).
To fix a violation of this rule, convert the assertion to Assert.IsType
(or Assert.IsNotType
) using exactMatch: false
.
using Xunit;
public class xUnit2032
{
[Fact]
public void TestMethod()
{
Assert.IsAssignableFrom<object>("Hello world");
}
}
using Xunit;
public class xUnit2032
{
[Fact]
public void TestMethod()
{
Assert.IsNotAssignableFrom<object>("Hello world");
}
}
using Xunit;
public class xUnit2032
{
[Fact]
public void TestMethod()
{
Assert.IsType<object>("Hello world", exactMatch: false);
}
}
using Xunit;
public class xUnit2032
{
[Fact]
public void TestMethod()
{
Assert.IsNotType<object>("Hello world", exactMatch: false);
}
}