【问题标题】:.net core asp.net unit tests that depend on files - appsettings.json - fail in travis.net core asp.net 单元测试依赖于文件 - appsettings.json - 在 travis 中失败
【发布时间】:2016-11-20 11:28:48
【问题描述】:

我创建了一个 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 上,我收到以下错误: Error : Speakr.WebApp.Site.Tests.InMemoryTests.HomeControllerIndexShouldNotBeNull

有人看过吗? 我尝试将 project.json 添加到测试项目中,在主项目中包含 CopyToOutput 并在测试项目中包含 CopytoOutput。

纳达! :(

【问题讨论】:

  • 附带说明:从技术上讲,这不是单元测试,而是集成测试:P
  • 是的,但是,这些测试对我来说更有用,因为控制器->服务->客户端层非常简单,无需测试,但是这些测试允许您测试管道。所以我一直“倾向于”称它们为“我的”单元测试,但你是绝对正确的:)

标签: json asp.net-mvc nunit asp.net-core travis-ci


【解决方案1】:

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

【讨论】:

  • 嗨,昨天开始在这个解决方案上达到顶峰,这可能是答案。
  • @jplebre 你有没有找到解决这个问题的方法?请提供您的答案 我正在努力解决同样的问题。
  • @LP13 仍然没有运气。上面的答案应该可以做到,但它过于复杂。 .net 文档建议我们应该能够在没有该样板的情况下进行集成测试。仍在努力让它发挥作用,但真的不能花太多时间在它上面:(
  • @LP13 "buildOptions": { "copyToOutput": [ "appsettings.json" ] } 会解决这个问题。看我的回答
【解决方案2】:

你应该添加

"buildOptions": { "copyToOutput": [ "appsettings.json" ] }

到您的集成测试项目 project.json, 然后.SetBasePath(env.ContentRootPath) 将起作用,因为在运行测试时 - appsettings.json 将位于 \test\IntegrationTestsProject\bin\Debug\netcoreapp1.1 - 启动查找它的位置。

【讨论】:

  • 我认为这是最好的解决方案。在较新的版本(没有 project.json)中,这归结为右键单击文件并将其设置为“复制到输出”
【解决方案3】:

经过一系列的尝试,我找到了一个可能的解决方案。由于我需要数据库启动,我派生了主要的StartupTestStartup 中的几个虚拟函数用于启动数据库。然后我在主要的Startup 类受保护的可设置中创建Configuration。最后:

public TestStartup(IHostingEnvironment env) : base(env)
{
    string basePath = Directory.GetCurrentDirectory();
    if (!Directory.GetCurrentDirectory().EndsWith("IntegrationTest")) {
        basePath = Path.Combine(basePath, "MyProject.IntegrationTest");
    }

    var builder = new ConfigurationBuilder();
    builder.SetBasePath(basePath)
           .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
           .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);

    builder.AddEnvironmentVariables();
    Configuration = builder.Build();
}

虽然basePath 获取有点难看,但这个解决方案对我有用。

【讨论】:

    猜你喜欢
    • 2021-07-17
    • 2017-05-13
    • 2016-11-05
    • 1970-01-01
    • 2020-07-18
    • 1970-01-01
    • 2012-07-15
    • 2020-08-04
    • 1970-01-01
    相关资源
    最近更新 更多