AspNetCore v2.0 — 在另一个项目中呈现 razor-views 以进行集成测试
AspNetCore v2.0 — Render razor-views in another project for integration tests
我想为我的网络应用程序编写集成测试。我使用 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>
。我还尝试按照 Microsoft 文档 (here in here) 中的描述重写代码。
但是我仍然有同样的错误信息。
重现步骤:
- 创建测试项目“
WebApplication1.IntegrationTests
”。
- 添加对 WebApplication-project 的引用。
- 添加 nuget 包“
Microsoft.AspNetCore.TestHost
”
- 通过
TestServer
向“/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
:
如果您使用的是 AspNetCore v2.0,您应该像这样在您的程序文件中实例化您的虚拟主机
public static IWebHost BuildWebHost(string[] args) {
return WebHost.CreateDefaultBuilder(args) //<-- See what this does
.UseStartup<Startup>()
.Build();
}
然后确保您的项目像您所做的那样将 preservecompilationcontext 设置为 true。另外,我假设您的目标是 .Net 4。6.x 运行时。
为了最近测试一个 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)
方法中使用了完整路径。
我想为我的网络应用程序编写集成测试。我使用 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>
。我还尝试按照 Microsoft 文档 (here in here) 中的描述重写代码。
但是我仍然有同样的错误信息。
重现步骤:
- 创建测试项目“
WebApplication1.IntegrationTests
”。 - 添加对 WebApplication-project 的引用。
- 添加 nuget 包“
Microsoft.AspNetCore.TestHost
” - 通过
TestServer
向“/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
:
如果您使用的是 AspNetCore v2.0,您应该像这样在您的程序文件中实例化您的虚拟主机
public static IWebHost BuildWebHost(string[] args) {
return WebHost.CreateDefaultBuilder(args) //<-- See what this does
.UseStartup<Startup>()
.Build();
}
然后确保您的项目像您所做的那样将 preservecompilationcontext 设置为 true。另外,我假设您的目标是 .Net 4。6.x 运行时。
为了最近测试一个 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)
方法中使用了完整路径。