C# 集成测试ASP.NET核心MVC时出现编译失败异常

C# 集成测试ASP.NET核心MVC时出现编译失败异常,c#,asp.net-core-mvc,C#,Asp.net Core Mvc,我想集成测试一个ASP.NET核心MVC网站。我首先添加了一个空的MVC WebApplication项目: <Project Sdk="Microsoft.NET.Sdk.Web"> <PropertyGroup> <TargetFramework>netcoreapp2.0</TargetFramework> </PropertyGroup> <ItemGroup> <PackageRe

我想集成测试一个ASP.NET核心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>
<Copy SourceFiles="%(DepsFilePaths.FullPath)" DestinationFolder="$(OutputPath)" Condition="Exists('%(DepsFilePaths.FullPath)')" />

netcoreapp2.0

<Copy SourceFiles="%(DepsFilePaths.FullPath)" DestinationFolder="$(OutputPath)" Condition="Exists('%(DepsFilePaths.FullPath)')" />
它运行并显示ASP.NET核心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>
<Copy SourceFiles="%(DepsFilePaths.FullPath)" DestinationFolder="$(OutputPath)" Condition="Exists('%(DepsFilePaths.FullPath)')" />

netcoreapp2.0
假的
在这里,我添加了nuget包Microsoft.AspNetCore.TestHost

<Copy SourceFiles="%(DepsFilePaths.FullPath)" DestinationFolder="$(OutputPath)" Condition="Exists('%(DepsFilePaths.FullPath)')" />
以下测试因CompliationFailedException而失败:

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("/");
    }
}
<Copy SourceFiles="%(DepsFilePaths.FullPath)" DestinationFolder="$(OutputPath)" Condition="Exists('%(DepsFilePaths.FullPath)')" />
使用Microsoft.AspNetCore.Hosting;
使用Microsoft.AspNetCore.TestHost;
使用WebApplication1;
使用Xunit;
公共类UnitTest1
{
[事实]
公共异步void Test1()
{
var builder=新的WebHostBuilder()
.UseContentRoot(@“C:\path\to\WebApplication1”)
.UseStartup();
var服务器=新的测试服务器(构建器);
var client=server.CreateClient();
var=wait client.GetAsync(“/”);
}
}
例外情况详情:

<Copy SourceFiles="%(DepsFilePaths.FullPath)" DestinationFolder="$(OutputPath)" Condition="Exists('%(DepsFilePaths.FullPath)')" />
Microsoft.AspNetCore.Mvc.Razor.Compilation.CompilationFailedException:发生一个或多个编译失败:
oxhek45x.0i3(4,62):错误CS0012:在未引用的程序集中定义了类型“Attribute”。必须添加对程序集“netstandard,Version=2.0.0.0,Culture=neutral,PublicKeyToken=CC7B13FFCD2DD51”的引用。
oxhek45x.0i3(4,81):错误CS0518:未定义或导入预定义类型“System.String”
oxhek45x.0i3(4110):错误CS0518:未定义或导入预定义类型“System.type”
oxhek45x.0i3(4,11):错误CS0518:未定义或导入预定义类型“System.Void”

<Copy SourceFiles="%(DepsFilePaths.FullPath)" DestinationFolder="$(OutputPath)" Condition="Exists('%(DepsFilePaths.FullPath)')" />
并描述类似的问题

<Copy SourceFiles="%(DepsFilePaths.FullPath)" DestinationFolder="$(OutputPath)" Condition="Exists('%(DepsFilePaths.FullPath)')" />
我尝试将此添加到测试项目中,但没有帮助:

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

我尝试替换MetadataReferenceFeature,如中所述。

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

<Copy SourceFiles="%(DepsFilePaths.FullPath)" DestinationFolder="$(OutputPath)" Condition="Exists('%(DepsFilePaths.FullPath)')" />
  • 将内容根目录设置为web项目的文件夹
  • 将*.deps.json文件从web项目的输出文件夹复制到测试项目的输出文件夹
  • 这一过程将在未来变得更容易。找到了解决办法。有关2.1中新功能的更多详细信息

    <Copy SourceFiles="%(DepsFilePaths.FullPath)" DestinationFolder="$(OutputPath)" Condition="Exists('%(DepsFilePaths.FullPath)')" />
    
    说明如何自动执行步骤2:

    <Copy SourceFiles="%(DepsFilePaths.FullPath)" DestinationFolder="$(OutputPath)" Condition="Exists('%(DepsFilePaths.FullPath)')" />
    
    只需将其添加到您的测试csproj文件中:

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

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

    此解决方案可行,但升级到ASP.Net Core 2.1没有任何区别。Copy Shallow和*.deps.json是键
    <Copy SourceFiles="%(DepsFilePaths.FullPath)" DestinationFolder="$(OutputPath)" Condition="Exists('%(DepsFilePaths.FullPath)')" />