.net Dotnet 6集成测试缺少Deps文件

qni6mghb  于 2023-02-06  发布在  .NET
关注(0)|答案(4)|浏览(309)

Before I start, I've tried all suggestions from the following and none work:
Integration testing ASP.NET Core with .NET Framework - can't find deps.json
https://zimmergren.net/unable-to-find-deps-json-dotnet-azure-devops/
So I'm trying to write some integration tests for dotnet 6. However, my WebApplicationFactory throws the following error:
System.InvalidOperationException: Can't find '/repos/subscription-info-api/tests/SubscriptionInfoApi.Tests.Integration/bin/Debug/net6.0/...
System.InvalidOperationException Can't find '/repos/subscription-info-api/tests/SubscriptionInfoApi.Tests.Integration/bin/Debug/net6.0/testhost.deps.json'. This file is required for functional tests to run properly. There should be a copy of the file on your source project bin folder. If that is not the case, make sure that the property PreserveCompilationContext is set to true on your project file. E.g 'true'. For functional tests to work they need to either run from the build output folder or the testhost.deps.json file from your application's output directory must be copied to the folder where the tests are running on. A common cause for this error is having shadow copying enabled when the tests run. at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory 1.EnsureDepsFile() at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory 1.EnsureServer() at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory 1.CreateDefaultClient(DelegatingHandler[] handlers) at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory 1.CreateDefaultClient(Uri baseAddress, DelegatingHandler[] handlers) at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory 1.CreateClient(WebApplicationFactoryClientOptions options) at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory 1.CreateClient() at SubscriptionInfoApi.Tests.Integration.UnitTest1.Test1() in /repos/subscription-info-api/tests/SubscriptionInfoApi.Tests.Integration/UnitTest1.cs:line 14 at SubscriptionInfoApi.Tests.Integration.UnitTest1.Test1() in /repos/subscription-info-api/tests/SubscriptionInfoApi.Tests.Integration/UnitTest1.cs:line 16 at Xunit.Sdk.TestInvoker 1.<>c__DisplayClass48_0.<<InvokeTestMethodAsync>b__1>d.MoveNext() in /_/src/xunit.execution/Sdk/Frameworks/Runners/TestInvoker.cs:line 264 --- End of stack trace from previous location --- at Xunit.Sdk.ExecutionTimer.AggregateAsync(Func 1 asyncAction) in //src/xunit.execution/Sdk/Frameworks/ExecutionTimer.cs:line 48 at Xunit.Sdk.ExceptionAggregator.RunAsync(Func`1 code) in //src/xunit.core/Sdk/ExceptionAggregator.cs:line 90
My actual test code is extremely simple:

[Fact]
    public async Task Test1()
    {
        await using var app    = new WebApplicationFactory<Program>();
        using var client = app.CreateClient();
        var       res    = await (await client.GetAsync("/alive-test")).Content.ReadAsStringAsync();
        Assert.Equal("Alive!", res);
    }

As per the suggestions, I've made sure I'm directly referencing Microsoft.AspNetCore.Mvc.Testing -> 6.0.0 in my integration tests project. I've also tried the various tweaks to the .csproj files that were suggested but nothing seems to be working.
I'm stuck for things to try to debug this further, any ideas?

qacovj5a

qacovj5a1#

您可能在测试文件中为Program指定了错误的名称空间(就像我一样)。
我不得不在Program.cs文件的末尾(最后一行)添加以下内容,以使它对需要它的测试项目可见:

public partial class Program { }

以下是一个示例:minimal api testing example

8nuwlpux

8nuwlpux2#

我也有同样的问题,尽管原因完全不同。
我使用的是.net 6,但我故意选择使用一个实际上有Program.cs文件的实现。当我从官方MS集成测试指南复制代码时,我让VS拉入所有依赖项。用于解析Program.cs的依赖项不是我自己的(为了这个答案,PersonalSite),而是MS自己的实现之一:

当然是我的一个小错误,但也许我能帮上忙。
对于那些真正需要分部类实现技巧的人,我链接的MS integ测试指南列出了这样做的指导方针。

jbose2ul

jbose2ul3#

FWIW还有另一种方法也可以工作(不需要修改代码)。
在应用的.csproj文件中添加以下内容(假设测试项目名为“Tests”):

<ItemGroup>
        <InternalsVisibleTo Include="Tests" />
      </ItemGroup>

这允许Tests项目查看Program.cs文件。

4smxwvx5

4smxwvx54#

将API中的Program类访问说明符从internal更改为Public

相关问题