gpt4 book ai didi

c# nunit 异常集成测试

转载 作者:行者123 更新时间:2023-11-30 23:05:38 25 4
gpt4 key购买 nike

我创建了一个如下所示的测试:

[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 : Only an administrator can confirm a new user

但这肯定是我要测试的。它应该通过,因为它会抛出 ArgumentException 错误?

最佳答案

原来我只是在做错误的断言。因为委托(delegate)是异步的,所以我需要做一个异步检查。所以用这个替换我的断言:

Assert.ThrowsAsync<ArgumentException>(async () => await services.WhenCreateUserAsync("AAAB", "AAAB", "AAAB", "FIN", emailConfirmed: true));

解决了问题

关于c# nunit 异常集成测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48690399/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com