我試圖了解如何對 ASP.NET Core 6 Web API 控制器進行集成測驗。我已經嘗試按照我能找到的所有指南、SO 帖子和建議進行操作,但由于某種原因,我不斷遇到指南中未提及的錯誤。
事件控制器測驗.cs
namespace UnitTests.ProjectToBeTested.Controllers
{
public class EventControllerTests
{
[Fact]
public async Task EventController_Post_RespondsOkIfRequestContainsCorrectFeilds_SuccessAsync()
{
// Arrange
var application = new WebApplicationFactory<Program>();
var client = application.CreateClient();
...
ProjectToBeTested.csproj
...
<ItemGroup>
<InternalsVisibleTo Include="IntegrationTests" />
</ItemGroup>
...
這在運行測驗時拋出以下內容:
訊息:System.InvalidOperationException:在“Program”上找不到方法“public static IHostBuilder CreateHostBuilder(string[] args)”或“public static IWebHostBuilder CreateWebHostBuilder(string[] args)”。或者,可以擴展 WebApplicationFactory`1 并覆寫“CreateHostBuilder”或“CreateWebHostBuilder”以提供您自己的實體。
堆疊跟蹤:WebApplicationFactory
1.CreateWebHostBuilder() WebApplicationFactory1.EnsureServer() WebApplicationFactory1.CreateDefaultClient(DelegatingHandler[] handlers) WebApplicationFactory1.CreateDefaultClient(Uri baseAddress, DelegatingHandler[] handlers) WebApplicationFactory1.CreateClient(WebApplicationFactoryClientOptions options) WebApplicationFactory1.CreateClient() EventControllerTests.EventController_Post_RespondsOkIfRequestContainsCorrectFeilds_SuccessAsync() 第 17 行 --- 從上一個位置開始的堆疊跟蹤結束
這個 SO 帖子顯示為一個可能的解決方案,但使用的是使用 Startup 類的 pre-6。.NET 6 解決方案是什么?
如果我改為遵循“使用默認 WebApplicationFactory 的基本測驗”指南,我什至無法構建解決方案,因為測驗類建構式拋出
Error CS0051 Inconsistent accessibility: parameter type 'WebApplicationFactory' is less accessible than method 'EventControllerTests.EventControllerTests(WebApplicationFactory)' IntegrationTests C:\...\EventControllerTests.cs
uj5u.com熱心網友回復:
我無法重現這一點。我從 .NET 6 RC1 上的命令列創建了兩個新專案
dotnet new webapi -o webapi1
dotnet new xunit -o test1
dotnet new sln
網路API專案
我對 Web API 專案所做的唯一更改是將其添加到專案檔案中:
<ItemGroup>
<InternalsVisibleTo Include="test1" />
</ItemGroup>
Program.cs 保持不變:
using Microsoft.OpenApi.Models;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
builder.Services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new() { Title = "webapi2", Version = "v1" });
});
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "webapi2 v1"));
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
測驗專案
在測驗專案中,我加入一個參考webapi1和Microsoft.AspNetCore.Mvc.Testing包裝。
我改Unit1.cs到
using Microsoft.AspNetCore.Mvc.Testing;
using Xunit;
using webapi2;
namespace test1;
public class UnitTest1
{
[Fact]
public void Test1()
{
using var app = new WebApplicationFactory<Program>();
using var client=app.CreateClient();
}
}
專案編譯成功,測驗運行成功。
uj5u.com熱心網友回復:
解決方案:
這是由于測驗專案的 Microsoft.AspNetCore.Mvc.Testing 包使用了錯誤的版本(它使用的是版本 5.*)。確保您使用適合 .NET 6 的版本。截至目前,有一個適用于我的 6.0.0-rc.2.21480.10 版本。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/322264.html
標籤:c# asp.net-core xunit .net-6.0
