Unit test exception messages with xUnit

Better to use the Record.Exception method as it matches the AAA pattern:

    [Fact]
    public void Divide_TwoNumbers_ExpectException()
    {
        var sut = new Calculator();
        var exception = Record.Exception(() => sut.Divide(10, 0));
        Assert.IsType(typeof(DivideByZeroException), exception);
    }

Hope this helps ...


I think it is correct to test for both Exception type and message. And both are easy in xUnit:

var exception = Assert.Throws<AuthenticationException>(() => DoSomething());
Assert.Equal(message, exception.Message);

Something like this

 var ex = Record.Exception(() => DoSomeThing());
 Assert.IsType(typeof(ArgumentNullException), ex);
 Assert.True(ex.Message.Contains("Your exception message"));