4
votes

I want to integration test an ASP.NET Core MVC WebSite. I started by adding an empty MVC WebApplication project:

<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. Then I added an 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.

The following test fails with a 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:
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 and #2981 describe similar problems.

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.

1

1 Answers

2
votes

Found a "solution" that works for me:

  1. Set the Content Root to the folder of the web project
  2. Copy the *.deps.json file from the output folder of the web project to the output folder of the test project

This process will be made easier in ASP.NET Core 2.1. Found the solution here. More details about the new feature in 2.1 can be found here.

This article explains how to automize step 2:

Just add this in to your test csproj-file:

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