簡體   English   中英

ASP.NET Core 3.1 - WebApplicationFactory - WebApplicationFactoryContentRootAttribute 的正確用法是什么?

[英]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();

其中StartupMyNamespace.AssemblyName項目中的類。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM