C#C
C#3y ago
hunterlan

xUnit: Can't assert exceptions

Hello, there! I want to compare exception types, but I'm getting error.

Here's my code:
        var exception = await Assert.ThrowsAsync<ArgumentException>(() => _endpoint.GetReport("1.1.1.1", null));
        Assert.Equal("Api key shouldn't be empty.", exception.Message);


Here's an error:
System.ArgumentException: Api key shouldn't be empty.

System.ArgumentException
Api key shouldn't be empty.


Here's part of code which I want to test:

protected string ApiKey
    {
        get => _apiKey;
        init
        {
            if (string.IsNullOrWhiteSpace(value))
            {
                throw new ArgumentException("Api key shouldn't be empty.");
            }

            _apiKey = value;
        }
    }
Was this page helpful?