![](/img/trans.png)
[英]Custom WebApplicationFactory in integration testing not registering services in Startup.cs (ASP.NET Core 3.1)
[英]ASP.NET Core 3.1 - WebApplicationFactory - What is the correct usage for WebApplicationFactoryContentRootAttribute?
我正在學習如何在 ASP.NET Core 3.1 中使用 WebApplicationFactory。 特別是我試圖找出WebApplicationFactoryContentRoot的正確用法
我創建了一個示例Github存儲庫來演示我遇到的問題。
我正在嘗試使用 WebApplicationFactoryContentRoot 屬性為位於../../../Src/WebApp
的啟動引導程序類設置內容根。
當我對下面的代碼運行dotnet test
,它似乎沒有引用WebApplicationFactoryContentRootAttribute設置。
拋出System.IO.DirectoryNotFoundException
:“我的解決方案基礎目錄”/WebApp。
路徑應該是“我的解決方案基礎目錄”/Src/WebApp
如何讓派生的WebApplicationFactory識別WebApplicationFactoryContentRootAttribute
集成測試
using System.IO;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Autofac.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Mvc.Testing;
using Xunit;
using Xunit.Abstractions;
using WebApp;
using WebApp.S3.Contracts;
using WebApp.Functional.Utilities;
/// Key should match FullName of assembly containing TStartup : WebApplicationFactory<TStartup>
/// https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.testing.webapplicationfactorycontentrootattribute?view=aspnetcore-3.0
[assembly: WebApplicationFactoryContentRootAttribute(
"WebApp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null",
"../../../Src/WebApp",
"Program.cs",
"1")]
namespace WebApp.FunctionalTests
{
/// <summary>
/// Eventual Goal: Tryng to create WebApplicationFactory with content root at Src/WebApp using
/// appsettings file derived from ASPNETCORE_ENVIRONMENT variable. If
/// the variable is unset then defaults to appsettings.Local.json
/// </summary>
public class AppTestFixture : WebApplicationFactory<Startup>
{
//override methods here as needed for Test purpose
}
public class ApiIntegrationTest : IClassFixture<AppTestFixture>
{
private WebApplicationFactory<Startup> _factory;
private ITestOutputHelper _output;
/// <summary>
/// Initialise derived WebApplicationFactory and output stream
/// </summary>
/// <param name="factory"><see cref="AppTestFixture"/>, dervied WebApplicationFactory</param>
/// <param name="output">xUnit output stream</param>
public ApiIntegrationTest(AppTestFixture factory, ITestOutputHelper output)
{
const string contentRoot = "Src/WebApp";
_factory = factory;
_output = output;
// sanity check for FullName property of assembly containing Startup class
string fullName = Assembly.GetAssembly(typeof(Startup)).FullName;
_output.WriteLine($"FullName property of assembly containing 'Startup' class => {fullName}");
}
/// <summary>
/// /Users/me/Development/dotnet/blazormotiondetectionlistener/WebApp/ not found
///
/// Should be path /Users/me/Development/dotnet/blazormotiondetectionlistener/Src/WebApp/
/// </summary>
///
/// <remarks>
/// How do I use WebApplicationFactoryContentRoot attribute to correctly resolve this?
/// </remarks>
[Fact]
public async Task WebApp_App_ApiController_Test()
{
var client = _factory.CreateClient();
await Task.CompletedTask;
}
}
}
測試項目
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<Content Update="xunit.runner.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Autofac" Version="6.0.0" />
<PackageReference Include="Microsoft.AspNetCore" Version="2.2.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" />
<PackageReference Include="coverlet.collector" Version="1.0.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\Src\WebApp\WebApp.csproj" />
<ProjectReference Include="..\..\Functional\Utilities\WebApp.Functional.Utilities.csproj" />
<ProjectReference Include="..\..\..\Src\WebApp.S3.Contracts\WebApp.S3.Contracts.csproj" />
</ItemGroup>
</Project>
該問題可能是您的文件引用Program.cs
當它應該是其他組件的DLL(即WebApp.dll
) -因為在你的碼頭工人集裝箱不會有任何*.cs
文件,如果是基於構建輸出。
在測試項目中(在您已經擁有的測試文件中,或者在AssemblyInfo.cs
如果有多個測試文件):
[assembly: WebApplicationFactoryContentRoot("MyNamespace.AssemblyName, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null", "./", "MyNamespace.AssemblyName.dll", "1")]
在您的測試課程中:
private readonly WebApplicationFactory<Startup> applicationFactory = new();
其中Startup
是MyNamespace.AssemblyName
項目中的類。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.