Reputation: 13397
I have a test I created which looks like this:
[Test]
public async Task MemberCannotCreateConfirmedUser()
{
var services = UserContext.GivenServices();
await services.LoginAsync(new Claim("User", "Member"));
Assert.That(await services.WhenCreateUserAsync("AAAB", "AAAB", "AAAB", "FIN", emailConfirmed: true), Throws.TypeOf<ArgumentException>());
}
And the code that actually throws the error looks like this:
var principal = _httpContext.User as ClaimsPrincipal;
var isAdmin = principal?.FindFirst("Administrator") != null;
if (!isAdmin)
{
if (model.EmailConfirmed) throw new ArgumentException("Only an administrator can confirm a new user");
}
When I run the test, it fails and states:
System.ArgumentException : Only an administrator can confirm a new user
But surely that is what I am testing for. It should pass because it throws an ArgumentException
error?
Upvotes: 2
Views: 611
Reputation: 13397
Turns out I was just doing to assert incorrectly. Because the delegate is asynchronous, I need to do an async check. So replacing my Assert with this:
Assert.ThrowsAsync<ArgumentException>(async () => await services.WhenCreateUserAsync("AAAB", "AAAB", "AAAB", "FIN", emailConfirmed: true));
Fixed the issue
Upvotes: 2