Razor compilation fails on TestHost when rendering view xUnit tests

260 views Asked by At

I following steps in the suggested workarounds here, here and and here

I added:

<PreserveCompilationContext>true</PreserveCompilationContext>

and

<!--
Work around https://github.com/NuGet/Home/issues/4412. MVC uses DependencyContext.Load()         which looks next to a .dll
for a .deps.json. Information isn't available elsewhere. Need the .deps.json file for all web site     applications. -->
  <Target Name="CopyDepsFiles" AfterTargets="Build" Condition="'$(TargetFramework)'!=''">
    <ItemGroup>
      <DepsFilePaths Include="$([System.IO.Path]::ChangeExtension('%(_ResolvedProjectReferencePaths.FullPath)', '.deps.json'))" />
    </ItemGroup>

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

and

 <ItemGroup>
    <None Include="xunit.runner.json" CopyToOutputDirectory="PreserveNewest" />
  </ItemGroup>

The xunit.runner.json looks like

{
  "shadowCopy": false
}

and

services.Configure((RazorViewEngineOptions options) =>
        {
            var previous = options.CompilationCallback;
            options.CompilationCallback = (context) =>
            {
                previous?.Invoke(context);

                var assembly = typeof(Startup).GetTypeInfo().Assembly;
                var assemblies = assembly.GetReferencedAssemblies().Select(x => MetadataReference.CreateFromFile(Assembly.Load(x).Location)).ToList();
                assemblies.Add(MetadataReference.CreateFromFile(Assembly.Load(new AssemblyName("mscorlib")).Location));
                assemblies.Add(MetadataReference.CreateFromFile(Assembly.Load(new AssemblyName("System.Private.Corelib")).Location));
                assemblies.Add(MetadataReference.CreateFromFile(Assembly.Load(new AssemblyName("System.IO")).Location));
                assemblies.Add(MetadataReference.CreateFromFile(Assembly.Load(new AssemblyName("SkyBankFinancial.Dal")).Location));
                assemblies.Add(MetadataReference.CreateFromFile(Assembly.Load(new AssemblyName("System.Linq")).Location));
                assemblies.Add(MetadataReference.CreateFromFile(Assembly.Load(new AssemblyName("System.Threading.Tasks")).Location));
                assemblies.Add(MetadataReference.CreateFromFile(Assembly.Load(new AssemblyName("System.Runtime")).Location));
                assemblies.Add(MetadataReference.CreateFromFile(Assembly.Load(new AssemblyName("System.Dynamic.Runtime")).Location));
                assemblies.Add(MetadataReference.CreateFromFile(Assembly.Load(new AssemblyName("Microsoft.AspNetCore.Razor.Runtime")).Location));
                assemblies.Add(MetadataReference.CreateFromFile(Assembly.Load(new AssemblyName("Microsoft.AspNetCore.Mvc")).Location));
                assemblies.Add(MetadataReference.CreateFromFile(Assembly.Load(new AssemblyName("Microsoft.AspNetCore.Razor")).Location));
                assemblies.Add(MetadataReference.CreateFromFile(Assembly.Load(new AssemblyName("Microsoft.AspNetCore.Mvc.Razor")).Location));
                assemblies.Add(MetadataReference.CreateFromFile(Assembly.Load(new AssemblyName("Microsoft.AspNetCore.Html.Abstractions")).Location));
                assemblies.Add(MetadataReference.CreateFromFile(Assembly.Load(new AssemblyName("System.Text.Encodings.Web")).Location));

                context.Compilation = context.Compilation.AddReferences(assemblies);
            };
        });

I am using:

VS2017 V15.5.3

Microsoft.AspNetCore.TestHost 1.1.3

netcoreapp1.1

Running test that returns View throws exception:

Message: Microsoft.AspNetCore.Mvc.Razor.Compilation.CompilationFailedException : One or more compilation failures occurred:
error CS0518: Predefined type 'System.Void' is not defined or imported
error CS0518: Predefined type 'System.Void' is not defined or imported
error CS0518: Predefined type 'System.Boolean' is not defined or imported
...

Any ideas what I have done wrong?

1

There are 1 answers

0
Kos On

After a little bit more research I found the answer here

So just adding a new file 'xunit.runner.json' to the test project containg the following code:

{
  "shadowCopy": false
}

and the following in the test csproj file

 <ItemGroup>
    <None Include="xunit.runner.json" CopyToOutputDirectory="PreserveNewest" />
  </ItemGroup>

  <!-- Solves Problem#1 (binding error) https://github.com/Microsoft/vstest/issues/428. -->
  <PropertyGroup>
    <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>

<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
  </PropertyGroup>

  <!--Work around https://github.com/NuGet/Home/issues/4412. MVC uses DependencyContext.Load()         which looks next to a .dll
for a .deps.json. Information isn't available elsewhere. Need the .deps.json file for all web site     applications.-->.

  <Target Name="CopyDepsFiles" AfterTargets="Build" Condition="'$(TargetFramework)'!=''">
    <ItemGroup>
      <DepsFilePaths Include="$([System.IO.Path]::ChangeExtension('%(_ResolvedProjectReferencePaths.FullPath)', '.deps.json'))" />
    </ItemGroup>

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

fixed the problem.