gpt4 book ai didi

c# - 集成测试 ASP.NET Core MVC 时出现 CompilationFailedException

转载 作者:行者123 更新时间:2023-11-30 17:30:48 26 4
gpt4 key购买 nike

我想对 ASP.NET Core MVC 网站进行集成测试。我首先添加一个空的 MVC WebApplication 项目:

<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.3" />
</ItemGroup>
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.1" />
</ItemGroup>
</Project>

它运行并显示 ASP.NET Core MVC 示例页面。然后我添加了一个 XUnit 项目:

<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="2.0.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0" />
<PackageReference Include="xunit" Version="2.3.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\WebApplication1\WebApplication1.csproj" />
</ItemGroup>
</Project>

我在其中添加了 nuget 包 Microsoft.AspNetCore.TestHost。

以下测试因 CompilationFailedException 而失败:

using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.TestHost;
using WebApplication1;
using Xunit;
public class UnitTest1
{
[Fact]
public async void Test1()
{
var builder = new WebHostBuilder()
.UseContentRoot(@"C:\path\to\WebApplication1")
.UseStartup<Startup>();

var server = new TestServer(builder);
var client = server.CreateClient();

var _ = await client.GetAsync("/");
}
}

异常详情:

Microsoft.AspNetCore.Mvc.Razor.Compilation.CompilationFailedException : One or more compilation failures occurred:
oxhek45x.0i3(4,62): error CS0012: The type 'Attribute' is defined in an assembly that is not referenced. You must add a reference to assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.
oxhek45x.0i3(4,81): error CS0518: Predefined type 'System.String' is not defined or imported
oxhek45x.0i3(4,110): error CS0518: Predefined type 'System.Type' is not defined or imported
oxhek45x.0i3(4,11): error CS0518: Predefined type 'System.Void' is not defined or imported

#542#2981描述类似的问题。

我尝试将其添加到测试项目中,但没有帮助:

<ItemGroup>
<Reference Include="netstandard" />
</ItemGroup>

而且我尝试按照 #2981 中的描述替换 MetadataReferenceFeature .

最佳答案

找到适合我的“解决方案”:

  1. 将 Content Root 设置为 Web 项目的文件夹
  2. 将web项目输出文件夹中的*.deps.json文件复制到测试项目输出文件夹

此过程将在 ASP.NET Core 2.1 中变得更容易.找到解决方案 here .有关 2.1 中新功能的更多详细信息 can be found here .

This article解释如何自动化第 2 步:

只需将其添加到您的测试 csproj 文件中:

<Copy SourceFiles="%(DepsFilePaths.FullPath)" DestinationFolder="$(OutputPath)" Condition="Exists('%(DepsFilePaths.FullPath)')" />

关于c# - 集成测试 ASP.NET Core MVC 时出现 CompilationFailedException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49078961/

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