Edit on GitHub

xUnit2014 Error

Do not use throws check to check for asynchronously thrown exception

Cause

This rule is triggered when calling Assert.Throws with an async lambda.

Reason for rule

Assert.Throws only supports non-async code.

How to fix violations

To fix a violation of this rule, use Assert.ThrowsAsync (along with await).

Examples

Violates

using System;
using System.Threading.Tasks;
using Xunit;

public class xUnit2014
{
    class MyMath
    {
        public static Task<int> Divide(params int[] values) => 42;
    }

    [Fact]
    public void TestMethod()
    {
        Assert.Throws<DivideByZeroException>(() => MyMath.Divide(1, 0));
    }
}

Does not violate

using System;
using System.Threading.Tasks;
using Xunit;

public class xUnit2014
{
    class MyMath
    {
        public static Task<int> Divide(params int[] values) => 42;
    }

    [Fact]
    public void TestMethod()
    {
        Assert.ThrowsAsync<DivideByZeroException>(() => MyMath.Divide(1, 0));
    }
}
Copyright © .NET Foundation. Contributions welcomed at https://github.com/xunit/xunit/tree/gh-pages.