ASP.NET Core TestServer results in HTTP 500 for Razor views

荒凉一梦 提交于 2019-12-23 13:02:43

问题


When I use the TestServer to call an MVC endpoint to check that the view renders, it results in a HTTP 500 Internal Server Error response.

The error is:

An error occurred during the compilation of a resource required to process this request. Please review the following specific error details and modify your source code appropriately.

/CustomSubfolder/Views/_ViewImports.cshtml

One or more compilation references are missing. Possible causes include a missing 'preserveCompilationContext' property under 'buildOptions' in the application's project.json.

The type or namespace name 'MyNamespace' does not exist in the namespace 'Company.App' (are you missing an assembly reference?)

Test code:

using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;

namespace MvcProject.Tests
{
    [TestClass]
    public class ControllerTests
    {
        protected TestServer Server { get; }
        protected HttpClient Client { get; }

        public ControllerTests()
        {
            Server = new TestServer(new WebHostBuilder()
                .UseContentRoot("../../../../MvcProject")
                .UseStartup<Startup>());
            Client = Server.CreateClient();
        }

        [TestMethod]
        public async Task Action_Valid_Renders()
        {
            HttpResponseMessage response = await Client.GetAsync("http://localhost/");

            Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
        }
    }
}

I'm using ASP.NET Core 1.1 targeting .NET Framework 4.6.1, and my MSTest .csproj file looks like:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net461</TargetFramework>
    <IsPackable>false</IsPackable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.TestHost" Version="1.1.3" />
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0" />
    <PackageReference Include="MSTest.TestAdapter" Version="1.1.18" />
    <PackageReference Include="MSTest.TestFramework" Version="1.1.18" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\MvcProject\MvcProject.csproj" />
  </ItemGroup>

</Project>

回答1:


As explained in https://github.com/aspnet/Razor/issues/1212, the problem is that .deps.json files required for Razor view compilation are not automatically copied over to the output of the test project.

You can add the below manual build step to the test project to resolve the issue.

  <!--
    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>


来源:https://stackoverflow.com/questions/46464373/asp-net-core-testserver-results-in-http-500-for-razor-views

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!