首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >xUnit TestServer MVC核心

xUnit TestServer MVC核心
EN

Stack Overflow用户
提问于 2018-07-04 14:11:39
回答 1查看 538关注 0票数 2

我试图设置一个测试,以获得我的所有产品,但我得到和ArgumentNullException,但我不明白为什么,我最近开始研究这个所以.

这是错误消息

消息: System.ArgumentNullException :值不能为空。 参数名称: connectionString

代码语言:javascript
复制
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);
}

提前感谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-05 06:48:41

消息: System.ArgumentNullException :值不能为空。参数名称: connectionString

对于此错误,这是由于您在运行TestServer时没有指定配置。在产品项目中,Microsoft.Extensions.Configuration.IConfiguration中的WebHost.CreateDefaultBuilder(args)将从'appsettings.json'中配置load

如果要使用生产appsettings.json进行测试,可以尝试如下所示:

代码语言:javascript
复制
        public ProductControllerIntegrationTests()
    {
        var configuration = new ConfigurationBuilder()
            .AddJsonFile("appsettings.json")
            .Build();

        server = new TestServer(new WebHostBuilder()
            .UseConfiguration(configuration)
            .UseStartup<Startup>()
            );
        client = server.CreateClient();
    }
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51175709

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档