Home > Mobile >  How do I assert on properties of an exception in XUnit?
How do I assert on properties of an exception in XUnit?

Time:10-19

I want to assert that an exception has a particular property set, but I can't work out a better way to do it than this:

    try {
        _ = await Repo.GetUserEmailVerificationCode(user3.Id);
        Assert.True(false);

    }
    catch (InvalidRequestException ex) when (ex.Tag == "EXPIRED") {
        Assert.True(true);

    }
    catch {
        Assert.True(false);

    }

I can do this, but I can see no way to check the Tag property on my Exception:

await Assert.ThrowsAsync<InvalidRequestException>(() => Repo.GetUserEmailVerificationCode(user3.Id));

Is there a XUnit assertion that can do this in a more concise single line Assert?

CodePudding user response:

If I'm understanding correctly, you could just assign that assertion to a variable.

var ex = await Assert.ThrowsAsync<InvalidRequestException>(() => Repo.GetUserEmailVerificationCode(user3.Id));

And then do another assertion

Assert.Equal("blah", ex.Tag);
  • Related