Loading...
Loading...
ASP.NET Core WebApi 整合測試完整指南。當需要對 WebApi 端點進行整合測試或驗證 ProblemDetails 錯誤格式時使用。涵蓋 WebApplicationFactory、IExceptionHandler、Testcontainers 多容器編排、Flurl URL 建構與 AwesomeAssertions HTTP 驗證。 Keywords: webapi integration testing, WebApplicationFactory, asp.net core integration test, webapi 整合測試, IExceptionHandler, ProblemDetails, ValidationProblemDetails, AwesomeAssertions, Flurl, Respawn, Be201Created, Be400BadRequest, 多容器測試, Collection Fixture, 全域例外處理
npx skill4agent add kevintsengtw/dotnet-testing-agent-skills dotnet-testing-advanced-webapi-integration-testingIExceptionHandlerProblemDetailsValidationProblemDetailsIExceptionHandler/// <summary>
/// 全域異常處理器
/// </summary>
public class GlobalExceptionHandler : IExceptionHandler
{
private readonly ILogger<GlobalExceptionHandler> _logger;
public GlobalExceptionHandler(ILogger<GlobalExceptionHandler> logger)
{
_logger = logger;
}
public async ValueTask<bool> TryHandleAsync(
HttpContext httpContext,
Exception exception,
CancellationToken cancellationToken)
{
_logger.LogError(exception, "發生未處理的異常: {Message}", exception.Message);
var problemDetails = CreateProblemDetails(exception);
httpContext.Response.StatusCode = problemDetails.Status ?? 500;
httpContext.Response.ContentType = "application/problem+json";
await httpContext.Response.WriteAsJsonAsync(problemDetails, cancellationToken);
return true;
}
private static ProblemDetails CreateProblemDetails(Exception exception)
{
return exception switch
{
KeyNotFoundException => new ProblemDetails
{
Type = "https://httpstatuses.com/404",
Title = "資源不存在",
Status = 404,
Detail = exception.Message
},
ArgumentException => new ProblemDetails
{
Type = "https://httpstatuses.com/400",
Title = "參數錯誤",
Status = 400,
Detail = exception.Message
},
_ => new ProblemDetails
{
Type = "https://httpstatuses.com/500",
Title = "內部伺服器錯誤",
Status = 500,
Detail = "發生未預期的錯誤"
}
};
}
}| 欄位 | 說明 |
|---|---|
| 問題類型的 URI |
| 簡短的錯誤描述 |
| HTTP 狀態碼 |
| 詳細的錯誤說明 |
| 發生問題的實例 URI |
{
"type": "https://tools.ietf.org/html/rfc9110#section-15.5.1",
"title": "One or more validation errors occurred.",
"status": 400,
"detail": "輸入的資料包含驗證錯誤",
"errors": {
"Name": ["產品名稱不能為空"],
"Price": ["產品價格必須大於 0"]
}
}IExceptionHandlerValidationExceptionValidationProblemDetails📖 完整實作程式碼請參閱 references/exception-handler-details.md
public class TestWebApplicationFactory : WebApplicationFactory<Program>, IAsyncLifetime
{
private PostgreSqlContainer? _postgresContainer;
private RedisContainer? _redisContainer;
private FakeTimeProvider? _timeProvider;
public PostgreSqlContainer PostgresContainer => _postgresContainer
?? throw new InvalidOperationException("PostgreSQL container 尚未初始化");
public RedisContainer RedisContainer => _redisContainer
?? throw new InvalidOperationException("Redis container 尚未初始化");
public FakeTimeProvider TimeProvider => _timeProvider
?? throw new InvalidOperationException("TimeProvider 尚未初始化");
public async Task InitializeAsync()
{
_postgresContainer = new PostgreSqlBuilder()
.WithImage("postgres:16-alpine")
.WithDatabase("test_db")
.WithUsername("testuser")
.WithPassword("testpass")
.WithCleanUp(true)
.Build();
_redisContainer = new RedisBuilder()
.WithImage("redis:7-alpine")
.WithCleanUp(true)
.Build();
_timeProvider = new FakeTimeProvider(new DateTimeOffset(2024, 1, 1, 0, 0, 0, TimeSpan.Zero));
await _postgresContainer.StartAsync();
await _redisContainer.StartAsync();
}
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.ConfigureAppConfiguration(config =>
{
config.Sources.Clear();
config.AddInMemoryCollection(new Dictionary<string, string?>
{
["ConnectionStrings:DefaultConnection"] = PostgresContainer.GetConnectionString(),
["ConnectionStrings:Redis"] = RedisContainer.GetConnectionString(),
["Logging:LogLevel:Default"] = "Warning"
});
});
builder.ConfigureServices(services =>
{
// 替換 TimeProvider
services.Remove(services.Single(d => d.ServiceType == typeof(TimeProvider)));
services.AddSingleton<TimeProvider>(TimeProvider);
});
builder.UseEnvironment("Testing");
}
public new async Task DisposeAsync()
{
if (_postgresContainer != null) await _postgresContainer.DisposeAsync();
if (_redisContainer != null) await _redisContainer.DisposeAsync();
await base.DisposeAsync();
}
}[CollectionDefinition("Integration Tests")]
public class IntegrationTestCollection : ICollectionFixture<TestWebApplicationFactory>
{
public const string Name = "Integration Tests";
}[Collection("Integration Tests")]
public abstract class IntegrationTestBase : IAsyncLifetime
{
protected readonly TestWebApplicationFactory Factory;
protected readonly HttpClient HttpClient;
protected readonly DatabaseManager DatabaseManager;
protected readonly IFlurlClient FlurlClient;
protected IntegrationTestBase(TestWebApplicationFactory factory)
{
Factory = factory;
HttpClient = factory.CreateClient();
DatabaseManager = new DatabaseManager(factory.PostgresContainer.GetConnectionString());
FlurlClient = new FlurlClient(HttpClient);
}
public virtual async Task InitializeAsync()
{
await DatabaseManager.InitializeDatabaseAsync();
}
public virtual async Task DisposeAsync()
{
await DatabaseManager.CleanDatabaseAsync();
FlurlClient.Dispose();
}
protected void ResetTime()
{
Factory.TimeProvider.SetUtcNow(new DateTimeOffset(2024, 1, 1, 0, 0, 0, TimeSpan.Zero));
}
protected void AdvanceTime(TimeSpan timeSpan)
{
Factory.TimeProvider.Advance(timeSpan);
}
}// 傳統方式
var url = $"/products?pageSize={pageSize}&page={page}&keyword={keyword}";
// 使用 Flurl
var url = "/products"
.SetQueryParam("pageSize", 5)
.SetQueryParam("page", 2)
.SetQueryParam("keyword", "特殊");[Fact]
public async Task CreateProduct_使用有效資料_應成功建立產品()
{
// Arrange
var request = new ProductCreateRequest { Name = "新產品", Price = 299.99m };
// Act
var response = await HttpClient.PostAsJsonAsync("/products", request);
// Assert
response.Should().Be201Created()
.And.Satisfy<ProductResponse>(product =>
{
product.Id.Should().NotBeEmpty();
product.Name.Should().Be("新產品");
product.Price.Should().Be(299.99m);
});
}[Fact]
public async Task CreateProduct_當產品名稱為空_應回傳400BadRequest()
{
// Arrange
var invalidRequest = new ProductCreateRequest { Name = "", Price = 100.00m };
// Act
var response = await HttpClient.PostAsJsonAsync("/products", invalidRequest);
// Assert
response.Should().Be400BadRequest()
.And.Satisfy<ValidationProblemDetails>(problem =>
{
problem.Type.Should().Be("https://tools.ietf.org/html/rfc9110#section-15.5.1");
problem.Title.Should().Be("One or more validation errors occurred.");
problem.Errors.Should().ContainKey("Name");
problem.Errors["Name"].Should().Contain("產品名稱不能為空");
});
}[Fact]
public async Task GetById_當產品不存在_應回傳404且包含ProblemDetails()
{
// Arrange
var nonExistentId = Guid.NewGuid();
// Act
var response = await HttpClient.GetAsync($"/Products/{nonExistentId}");
// Assert
response.Should().Be404NotFound()
.And.Satisfy<ProblemDetails>(problem =>
{
problem.Type.Should().Be("https://httpstatuses.com/404");
problem.Title.Should().Be("產品不存在");
problem.Status.Should().Be(404);
});
}[Fact]
public async Task GetProducts_使用分頁參數_應回傳正確的分頁結果()
{
// Arrange
await TestHelpers.SeedProductsAsync(DatabaseManager, 15);
// Act - 使用 Flurl 建構 QueryString
var url = "/products"
.SetQueryParam("pageSize", 5)
.SetQueryParam("page", 2);
var response = await HttpClient.GetAsync(url);
// Assert
response.Should().Be200Ok()
.And.Satisfy<PagedResult<ProductResponse>>(result =>
{
result.Total.Should().Be(15);
result.PageSize.Should().Be(5);
result.Page.Should().Be(2);
result.Items.Should().HaveCount(5);
});
}public static class TestHelpers
{
public static ProductCreateRequest CreateProductRequest(
string name = "測試產品",
decimal price = 100.00m)
{
return new ProductCreateRequest { Name = name, Price = price };
}
public static async Task SeedProductsAsync(DatabaseManager dbManager, int count)
{
var tasks = Enumerable.Range(1, count)
.Select(i => SeedSpecificProductAsync(dbManager, $"產品 {i:D2}", i * 10.0m));
await Task.WhenAll(tasks);
}
}tests/Integration/
└── SqlScripts/
└── Tables/
└── CreateProductsTable.sql<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="AwesomeAssertions" Version="9.1.0" />
<PackageReference Include="Testcontainers.PostgreSql" Version="4.0.0" />
<PackageReference Include="Testcontainers.Redis" Version="4.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="9.0.0" />
<PackageReference Include="Flurl" Version="4.0.0" />
<PackageReference Include="Respawn" Version="6.2.1" />src/
├── Api/ # WebApi 層
├── Application/ # 應用服務層
├── Domain/ # 領域模型
└── Infrastructure/ # 基礎設施層
tests/
└── Integration/
├── Fixtures/
│ ├── TestWebApplicationFactory.cs
│ ├── IntegrationTestCollection.cs
│ └── IntegrationTestBase.cs
├── Handlers/
│ ├── GlobalExceptionHandler.cs
│ └── FluentValidationExceptionHandler.cs
├── Helpers/
│ ├── DatabaseManager.cs
│ └── TestHelpers.cs
├── SqlScripts/
│ └── Tables/
└── Controllers/
└── ProductsControllerTests.cs