I have some cases where I don't care what exception is thrown (as long as some exception is thrown). Unfortunately,
Assert.Throws<Exception>(someDelegate); doesn't pass unless exactly an instance of Exception (so not an instance of a derived class) is thrown. I know I can obtain the behavior I want with
Exception exception = Record.Exception(someDelegate); Assert.NotNull(exception); but it doesn't read right. Am I missing something in xUnit that has the behavior I want? Here are two tests that indicate what I mean:
[Fact] public void Throws_exception_and_passes() { Exception exception = Record.Exception( () => { throw new InvalidOperationException(); } ); Assert.NotNull(exception); } [Fact] public void Throws_exception_and_fails() { Assert.Throws<Exception>( () => { throw new InvalidOperationException(); } ); }