我试图设置一个测试,以获得我的所有产品,但我得到和ArgumentNullException,但我不明白为什么,我最近开始研究这个所以.
这是错误消息
消息: System.ArgumentNullException :值不能为空。 参数名称: connectionString
private readonly TestServer server;
private readonly HttpClient client;
public ProductControllerIntegrationTests()
{
server = new TestServer(new WebHostBuilder()
.UseStartup<Startup>());
client = server.CreateClient();
}
[Fact]
public async Task Product_Get_All()
{
var response = await client.GetAsync("/api/Products");
response.EnsureSuccessStatusCode();
var responseString = await response.Content.ReadAsStringAsync();
var products = JsonConvert.DeserializeObject<IEnumerable<Product>>(responseString);
products.Count().Should().Be(12);
}提前感谢!
发布于 2018-07-05 06:48:41
消息: System.ArgumentNullException :值不能为空。参数名称: connectionString
对于此错误,这是由于您在运行TestServer时没有指定配置。在产品项目中,Microsoft.Extensions.Configuration.IConfiguration中的WebHost.CreateDefaultBuilder(args)将从'appsettings.json'中配置load 。
如果要使用生产appsettings.json进行测试,可以尝试如下所示:
public ProductControllerIntegrationTests()
{
var configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.Build();
server = new TestServer(new WebHostBuilder()
.UseConfiguration(configuration)
.UseStartup<Startup>()
);
client = server.CreateClient();
}https://stackoverflow.com/questions/51175709
复制相似问题