Do not use typeof expression to check the type
A violation of this rule occurs when the typeof
operator is used with a type checking assert.
When the expected type is known at compile-time, the generic overload should be used. In addition to being more concise, it also returns the value cast to the appropriate type when the assert succeeds, for use in later assertions.
Use the generic overload of Assert.IsType
, Assert.IsNotType
, or Assert.IsAssignableFrom
.
using Xunit;
public class xUnit2007
{
[Fact]
public void TestMethod()
{
var result = "foo";
Assert.IsType(typeof(string), result);
}
}
using Xunit;
public class xUnit2007
{
[Fact]
public void TestMethod()
{
var result = "foo";
Assert.IsType<string>(result);
}
}