Do not use typeof expression to check the exception type
This rule is triggered when using the non-generic version of Assert.Throws
along with a typeof
expression.
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 exception cast to the appropriate type when the assert succeeds, for use in later assertions.
To fix a violation of this rule, use the generic version of Assert.Throws
.
using System;
using Xunit;
public class xUnit2015
{
void FunctionThatThrows() { }
[Fact]
public void TestMethod()
{
Assert.Throws(typeof(InvalidOperationException), () => FunctionThatThrows());
}
}
using System;
using Xunit;
public class xUnit2015
{
void FunctionThatThrows() { }
[Fact]
public void TestMethod()
{
Assert.Throws<InvalidOperationException>(() => FunctionThatThrows());
}
}