ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

C#-AspNetCore v2.0-在另一个项目中渲染剃刀视图以进行集成测试

2019-11-10 19:08:27  阅读:323  来源: 互联网

标签:razorengine asp-net-core-mvc asp-net-core-2-0 asp-net c


我想为我的Web应用程序编写集成测试.我使用Microsoft.AspNetCore.TestHost.TestServer通过url与控制器进行通信.但是视图无法呈现.作为回应,我收到错误消息:

One or more compilation references are missing. Ensure that your
project is referencing ‘Microsoft.NET.Sdk.Web’ and the
‘PreserveCompilationContext’ property is not set to false.

在我的.csproj中,我尝试更改Microsoft.NET.Sdk.Web上的project-sdk,并尝试添加< PreserveCompilationContext> true< / PreserveCompilationContext&gt ;.我也尝试按照Microsoft-docs(here中的here)中所述重写代码.
但是我仍然有同样的错误信息.

重现步骤:

>创建测试项目“ WebApplication1.IntegrationTests”.
>在WebApplication-project上添加参考.
>添加nuget程序包“ Microsoft.AspNetCore.TestHost”
>通过TestServer将get-request发送到“ / Home / Index”.
>读取带有错误消息的响应.

using WebApplication1;
using Microsoft.AspNetCore.TestHost; 
using System.Net.Http;
using System.Threading.Tasks;

namespace WebApplication1.IntegrationTests
{
    public class Program
    {
        public static void Main(string[] args)
        {
            // Path to contentRoot folder. 
            var contentRootPath = @"..\..\..\..\WebApplication1";

            var builder = new WebHostBuilder()
                .UseStartup<Startup>()
                .UseEnvironment(EnvironmentName.Development)
                .UseContentRoot(contentRootPath);           

            var server = new TestServer(builder);
            var client = server.CreateClient();            

            var response = client.GetAsync("/Home/Index").Result;
            var responseString = response.Content.ReadAsStringAsync().Result;
        }
    }
}

在浏览器中呈现的responseString:
enter image description here

解决方法:

为了最近测试ASP.NET Core 2.0应用程序,我不得不使用< PreserveCompilationContext> true< / PreserveCompilationContext>更新集成测试项目的csproj文件.属性,并带有一个用于复制.deps.json文件的附加目标.

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

您可以检查完整集成测试项目in github的源代码.可能会有其他差异,例如我将完整路径与.UseContentRoot(path)方法一起使用.

标签:razorengine,asp-net-core-mvc,asp-net-core-2-0,asp-net,c
来源: https://codeday.me/bug/20191110/2014523.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有