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.