我试图用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,它运行平稳,没有连接问题。
startup.cs:
services.AddDbContext<BlogProjectContext>(option =>
option.UseSqlServer(Configuration.GetConnectionString("BlogProjectConnection"))
);
appsetings.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"
}
}
https://stackoverflow.com/questions/60976761
复制相似问题