Integration testing ASP.NET Core with .NET Framework - can#39;t find deps.json(使用 .NET Framework 集成测试 ASP.NET Core - 找不到 deps.json)
问题描述
我有一个面向 .NET Framework 4.7 的 ASP.NET Core Web API 项目,我正在尝试为其编写集成测试.我使用 Visual Studio 添加新项目和单元测试项目 (.NET Framework) 模板创建了一个单元测试项目.我将 Microsoft.AspNetCore.Mvc.Testing NuGet 包添加到测试项目中,我有以下测试:
I have a ASP.NET Core Web API project targeting .NET Framework 4.7 that I'm trying to write integration tests for. I created a unit test project using Visual Studio Add new project and then the Unit Test Project (.NET Framework) template. I added the Microsoft.AspNetCore.Mvc.Testing NuGet package to the test project, and I have the following test:
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");
}
}
}
但这会引发以下异常:
测试方法TestRepro.Tests.UnitTest1.TestMethod1抛出异常:System.InvalidOperationException:找不到'[路径已删除]TestRepro.TestsinDebugTestRepro.deps.json'.此文件是功能测试正常运行所必需的.您的源项目 bin 文件夹中应该有该文件的副本.如果不是这种情况,请确保在项目文件中将属性 PreserveCompilationContext 设置为 true.例如真实".要使功能测试正常工作,它们需要从构建输出文件夹运行,或者必须将应用程序输出目录中的 TestRepro.deps.json 文件复制到运行测试的文件夹中.此错误的常见原因是在测试运行时启用了卷影复制.
Test method TestRepro.Tests.UnitTest1.TestMethod1 threw exception: System.InvalidOperationException: Can't find'[path removed]TestRepro.TestsinDebugTestRepro.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.
我已验证 Web 应用程序输出文件夹 (TestReproinDebug et47) 中存在 TestRepro.deps.json,但它没有复制到测试项目输出文件夹 (TestRepro.TestsinDebug).而且我还没有找到如何禁用卷影复制.
I have verified that TestRepro.deps.json exists in the web application output folder (TestReproinDebug et47), but it is not copied to the test project output folder (TestRepro.TestsinDebug). And I have not been able to find out how to disable shadow copying.
文档说:
Microsoft.AspNetCore.Mvc.Testing 包处理以下任务:将依赖项文件 (*.deps) 从 SUT 复制到测试项目的 bin 文件夹中.
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 中进行构建步骤,但感觉很粗糙.有什么想法吗?
But that doesn't seem to work. I can copy the file manually, but that doesn't work in a automated build scenario. One way would be to have a build step doing it in TeamCity, but it feels crude. Any ideas?
如果有帮助的话,我有 GitHub 上的一个副本.
I have a repro on GitHub if that helps.
推荐答案
按照以下步骤为 Asp.Net Core 创建集成测试,目标为 net 47.
Follow steps below to create Integration Test for Asp.Net Core with targeting net 47.
- 创建
新建项目->xUnit 测试项目(.Net Core)
- 右键新建项目->编辑.csproj->将
TargetFramework
改为net47
- 将项目引用添加到
TestRepro
- 安装包
Microsoft.AspNetCore.Mvc.Testing
添加如下测试文件
- Create
New Project-> xUnit Test Project(.Net Core)
- Right click new project->Edit .csproj->Change
TargetFramework
tonet47
- Add Project Reference to
TestRepro
- Install-Package
Microsoft.AspNetCore.Mvc.Testing
Add Test file like below
public class BasicTests
: IClassFixture<WebApplicationFactory<Startup>>
{
private readonly WebApplicationFactory<Startup> _factory;
public BasicTests(WebApplicationFactory<Startup> factory)
{
_factory = factory;
}
[Fact]
public async Task TestMethod1()
{
var client = _factory.CreateClient();
var response = await client.GetAsync("/api/values");
}
}
运行测试项目
Run Test Project
这篇关于使用 .NET Framework 集成测试 ASP.NET Core - 找不到 deps.json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 .NET Framework 集成测试 ASP.NET Core - 找不到 deps.json
基础教程推荐
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- 将 XML 转换为通用列表 2022-01-01