public async Task AsyncThrowsExceptionExactlyOfStaticType() { async Task a() { await Task.Delay(1); throw new ArgumentNullException("id"); } Func <Task> action = a; ArgumentException ae; // MSTest ae = await MSTestAssert.ThrowsExceptionAsync <ArgumentException>(action, "Some context"); // Assert.ThrowsException failed. Threw exception ArgumentNullException, but exception ArgumentException was expected. Some context // Exception Message: Value cannot be null. // Parameter name: id // Stack Trace: ... // NUnit does not support this use case. // XUnit ae = await XUnitAssert.ThrowsAsync <ArgumentException>(action); // Assert.Throws() Failure // Expected: typeof(System.ArgumentException) // Actual: typeof(System.ArgumentNullException): Value cannot be null. // Parameter name: id // ---- System.ArgumentNullException: Value cannot be null. // Parameter name: id // Fluent ae = action.Should().ThrowExactly <ArgumentException>("SOME REASONS").Which; // Expected type to be System.ArgumentException because SOME REASONS, but found System.ArgumentNullException. // Shouldly does not support this case. }
/// <inheritDoc/> public static async Task <T> ThrowsExceptionAsync <T>(this Func <Task> action) where T : Exception { return(await Assert.ThrowsExceptionAsync <T>(action)); }