gpt4 book ai didi

.net - 集成测试 ASP.NET Core 与 .NET Framework - 找不到 deps.json

转载 作者:行者123 更新时间:2023-12-01 22:17:41 31 4
gpt4 key购买 nike

我有一个针对 .NET Framework 4.7 的 ASP.NET Core Web API 项目,我正在尝试为其编写集成测试。我使用 Visual Studio 添加新项目和单元测试项目 (.NET Framework) 模板创建了一个单元测试项目。我将 Microsoft.AspNetCore.Mvc.Testing NuGet 包添加到测试项目中,并进行了以下测试:

using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace TestRepro.Tests
{
[TestClass]
public class UnitTest1
{
[TestMethod]
public async Task TestMethod1()
{
var factory = new WebApplicationFactory<Startup>();
var client = factory.CreateClient();
var response = await client.GetAsync("/api/values");
}
}
}

但这会引发以下异常:

Test method TestRepro.Tests.UnitTest1.TestMethod1 threw exception: System.InvalidOperationException: Can't find'[path removed]\TestRepro.Tests\bin\Debug\TestRepro.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 TestRepro.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.

我已验证 TestRepro.deps.json 存在于 Web 应用程序输出文件夹 (TestRepro\bin\Debug\net47) 中,但未将其复制到测试项目输出文件夹 (TestRepro.Tests\bin\Debug)。而且我一直无法找到如何禁用卷影复制。

编辑: The documentation说:

The Microsoft.AspNetCore.Mvc.Testing package handles the following tasks: Copies the dependencies file (*.deps) from the SUT into the test project's bin folder.

但这似乎不起作用。我可以手动复制文件,但这在自动构建场景中不起作用。一种方法是在 TeamCity 中执行构建步骤,但感觉很粗糙。有什么想法吗?

我有a repro on GitHub如果有帮助的话。

最佳答案

我们应该从主项目中获取“Program”类,但它会自动引用 Microsoft.AspNetCore.Mvc.Testing.Program

public class VersioningTests : IClassFixture<WebApplicationFactory<Program>>

此代码必须引用我们的主程序代码。因此,我们必须在 Program.cs 文件的最后一行放置对 Program 类的引用:

public partial class Program { }

关于.net - 集成测试 ASP.NET Core 与 .NET Framework - 找不到 deps.json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55131379/

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