我有一个带有JWT身份验证的web API应用程序,我想为它写测试,我不知道使用XUnit或使用HTTP客户端调用API!
XUnit
[Fact]
public async Task Authenticate_WithValidUsernamePassword_ReturnsToken()
{
//...
}
Http客户端
[TestMethod]
public async Task Authenticate_WithValidUsernamePassword_ReturnsToken()
{
HttpClient _client =...
var httpResponse = await _client.GetAsync("api/v1/admin/Authenticate?....");
}
尝试xUnit如下:
[Fact]
public async Task Authenticate_WithValidUsernamePassword_ReturnsToken()
{
using var client = new HttpClient();
var content = await client.GetStringAsync("/api/v1/admin/Authenticate?....");
bool result = false;
if (content == "foo authentication ok") {
result = true;
}
Assert.True(result, $"foo authentication failed Result={result}");
}