gpt4 book ai didi

json - 依赖于文件的 .net 核心 asp.net 单元测试 - appsettings.json - 在 travis 中失败

转载 作者:行者123 更新时间:2023-12-04 14:41:18 25 4
gpt4 key购买 nike

我创建了一个 asp.net dot net core rtm (1.0.0-preview2-003121)。

它使用 ConfigurationBuilder 从 appsettings.json 生成配置:

public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);

Configuration = builder.Build();
}

我也试过'.SetBasePath(Directory.GetCurrentDirectory())`

现在我的单元测试(在另一个项目中)我正在构建一个内存中的主机:
我试过了:
_server = new TestServer(new WebHostBuilder().UseStartup<Startup>());
_client = _server.CreateClient();

我试过:
_server = new TestServer(
new WebHostBuilder()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>());
_client = _server.CreateClient();

我的 Travis.yml 文件非常标准:
 install:
# Install .net using linux CLI commands
- sudo sh -c 'echo "deb [arch=amd64] https://apt-mo.trafficmanager.net/repos/dotnet/ trusty main" > /etc/apt/sources.list.d/dotnetdev.list'
- sudo apt-key adv --keyserver apt-mo.trafficmanager.net --recv-keys 417A0893
- sudo apt-get update
- sudo apt-get -qq install dotnet-dev-1.0.0-preview2-003121
- sudo apt-get install dotnet-hostfxr-1.0.2

script:
- dotnet restore
- dotnet build src/Speakr.WebApp.Site
- dotnet build tests/Speakr.WebApp.Site.Tests
- dotnet test tests/Speakr.WebApp.Site.Tests -f netcoreapp1.0

在本地,所有工作和构建。在 Windows 和 Ubuntu 上。
在 travis CI tho 上,我收到以下错误: Error : Speakr.WebApp.Site.Tests.InMemoryTests.HomeControllerIndexShouldNotBeNull
有没有人看到这个?
我尝试将 project.json 添加到测试项目中,在主项目中包含 CopyToOutput,在测试项目中包含 CopytoOutput。

纳达! :(

最佳答案

MVC 自己的功能/集成测试是如何做到这一点的一个很好的例子。

MvcTestFixture :(注意'contentRoot'是计算出来的)
https://github.com/aspnet/Mvc/blob/1.0.0/test/Microsoft.AspNetCore.Mvc.FunctionalTests/MvcTestFixture.cs

如何在测试中使用它的示例 :
https://github.com/aspnet/Mvc/blob/1.0.0/test/Microsoft.AspNetCore.Mvc.FunctionalTests/BasicTests.cs

以上测试验证的网站或应用程序 :
https://github.com/aspnet/Mvc/tree/1.0.0/test/WebSites/BasicWebSite

关于json - 依赖于文件的 .net 核心 asp.net 单元测试 - appsettings.json - 在 travis 中失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38426847/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com