【发布时间】:2020-04-01 17:24:59
【问题描述】:
我正在尝试用 Xunit 编写单元测试:
public class UserTest
{
private readonly TestServer _server;
private readonly HttpClient _client;
public UserTest()
{
_server = new TestServer(new WebHostBuilder().UseStartup<Startup>());
_client = _server.CreateClient();
}
[Fact]
public async Task GetAllUserTest()
{
var request = new HttpRequestMessage(new HttpMethod("Get"), "/Api/Users");
var response =await _client.SendAsync(request);
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
}
}
请求验证在我的解决方案中。 测试运行后,测试失败,消息错误为:
值不能为空。 (参数'connectionString')
如果我运行api,它运行顺利,没有CONNECTION问题。
startup.cs:
services.AddDbContext<BlogProjectContext>(option =>
option.UseSqlServer(Configuration.GetConnectionString("BlogProjectConnection"))
);
appsettings.json:
{
"AppSettings": {
"Secret": "This is the secret key and its very important"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
"ConnectionStrings": {
"BlogProjectConnection": "Data Source=.;Initial Catalog=BlogProject_DB;Integrated Security=True"
}
}
【问题讨论】:
标签: unit-testing asp.net-core-webapi xunit xunit.net webapi