[英]CompilationFailedException when integration testing ASP.NET Core MVC
I want to integration test an ASP.NET Core MVC WebSite. 我想集成测试ASP.NET Core MVC WebSite。 I started by adding an empty MVC WebApplication project:
我首先添加了一个空的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>
It runs and shows the ASP.NET Core MVC sample page. 它运行并显示ASP.NET Core MVC示例页面。 Then I added an XUnit-Project:
然后我添加了一个XUnit-Project:
<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>
Where I added the nuget package Microsoft.AspNetCore.TestHost. 我在哪里添加了nuget包Microsoft.AspNetCore.TestHost。
The following test fails with a CompilationFailedException: 以下测试因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("/");
}
}
Exception details: 例外细节:
Microsoft.AspNetCore.Mvc.Razor.Compilation.CompilationFailedException : One or more compilation failures occurred:
Microsoft.AspNetCore.Mvc.Razor.Compilation.CompilationFailedException:发生一个或多个编译失败:
oxhek45x.0i3(4,62): error CS0012: The type 'Attribute' is defined in an assembly that is not referenced.oxhek45x.0i3(4,62):错误CS0012:类型“属性”在未引用的程序集中定义。 You must add a reference to assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.
您必须添加对程序集'netstandard,Version = 2.0.0.0,Culture = neutral,PublicKeyToken = cc7b13ffcd2ddd51'的引用。
oxhek45x.0i3(4,81): error CS0518: Predefined type 'System.String' is not defined or importedoxhek45x.0i3(4,81):错误CS0518:未定义或导入预定义类型'System.String'
oxhek45x.0i3(4,110): error CS0518: Predefined type 'System.Type' is not defined or importedoxhek45x.0i3(4,110):错误CS0518:未定义或导入预定义类型“System.Type”
oxhek45x.0i3(4,11): error CS0518: Predefined type 'System.Void' is not defined or importedoxhek45x.0i3(4,11):错误CS0518:未定义或导入预定义类型“System.Void”
#542 and #2981 describe similar problems. #542和#2981描述了类似的问题。
I tried adding this to the test project, but it didn't help: 我尝试将其添加到测试项目中,但它没有帮助:
<ItemGroup>
<Reference Include="netstandard" />
</ItemGroup>
And I tried replacing the MetadataReferenceFeature as described in #2981 . 我尝试按照#2981中的描述替换MetadataReferenceFeature。
Found a "solution" that works for me: 找到适合我的“解决方案”:
This process will be made easier in ASP.NET Core 2.1 . 在ASP.NET Core 2.1中,此过程将变得更容易。 Found the solution here .
在这里找到解决方案。 More details about the new feature in 2.1 can be found here .
有关2.1中新功能的更多详细信息,请参见此处 。
This article explains how to automize step 2: 本文介绍如何自动化第2步:
Just add this in to your test csproj-file: 只需将其添加到测试csproj文件中:
<Copy SourceFiles="%(DepsFilePaths.FullPath)" DestinationFolder="$(OutputPath)" Condition="Exists('%(DepsFilePaths.FullPath)')" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.