我创建的测试看起来像这样:
[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>());
}
实际抛出错误的代码如下所示:
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");
}
当我运行测试时,它失败并说明:
System.ArgumentException:只有管理员才能确认新用户
但这肯定是我正在测试的。它应该通过,因为它会引发ArgumentException
错误?
答案 0 :(得分:2)
原来我只是做错了断言。因为委托是异步的,所以我需要进行异步检查。所以用这个替换我的Assert:
Assert.ThrowsAsync<ArgumentException>(async () => await services.WhenCreateUserAsync("AAAB", "AAAB", "AAAB", "FIN", emailConfirmed: true));
修正了问题