© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
5 replies
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);
        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.
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;
        }
    }
protected string ApiKey
    {
        get => _apiKey;
        init
        {
            if (string.IsNullOrWhiteSpace(value))
            {
                throw new ArgumentException("Api key shouldn't be empty.");
            }

            _apiKey = value;
        }
    }
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Assert.Raises<> in xUnit
C#CC# / help
3y ago
Test ASP.API controller with xUnit: can't assert Type
C#CC# / help
3y ago
Extending the Assert class in xunit
C#CC# / help
2y ago
How to fix "Assert.IsAssignableFrom() Failure: Value is null" in Xunit testing?
C#CC# / help
3y ago