重构单元测试和集成测试

This commit is contained in:
administrator committed 2025-03-15 17:33:37 +08:00
1 parent e7e9afe11b
commit 81c8f608dc
34 files changed
+135 -300

No files matched your search

@@ -1,7 +1,7 @@
// Copyright (c) HelloShop Corporation. All rights reserved.
// See the license file in the project root for more information.
using HelloShop.ProductService.FunctionalTests.Utilities;
using HelloShop.ProductService.FunctionalTests.Helpers;
using HelloShop.ProductService.Models.Products;
using System.Net;
using System.Net.Http.Json;
@@ -31,8 +31,11 @@ namespace HelloShop.ProductService.FunctionalTests
// Act
HttpResponseMessage response = await client.GetAsync("api/Brands/1");
string responseContent = await response.Content.ReadAsStringAsync();
BrandDetailsResponse? brandDetails = await response.Content.ReadFromJsonAsync<BrandDetailsResponse>();
// Assert
Assert.NotNull(brandDetails);
Assert.Equal(1, brandDetails.Id);
@@ -7,40 +7,54 @@ using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Hosting;
using System.Data.Common;
using System.Net.Http.Headers;
namespace HelloShop.ProductService.FunctionalTests.Utilities
namespace HelloShop.ProductService.FunctionalTests.Helpers
{
public class CustomWebApplicationFactory<TProgram> : WebApplicationFactory<TProgram> where TProgram : class
{
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.ConfigureServices(services =>
builder.ConfigureServices(static services =>
{
ServiceDescriptor? dbContextDescriptor = services.SingleOrDefault(d => d.ServiceType == typeof(DbContextOptions<ProductServiceDbContext>));
var dbContextDescriptor = services.SingleOrDefault(d => d.ServiceType == typeof(DbContextOptions<ProductServiceDbContext>));
if (dbContextDescriptor != null)
{
services.Remove(dbContextDescriptor);
}
// Create open SqliteConnection so EF won't automatically close it.
services.AddSingleton(container =>
var dbConnectionDescriptor = services.SingleOrDefault(d => d.ServiceType == typeof(DbConnection));
if (dbConnectionDescriptor != null)
{
DbConnection connection = new SqliteConnection("DataSource=:memory:");
services.Remove(dbConnectionDescriptor);
}
var optionsConfigurationDescriptor = services.SingleOrDefault(d => d.ServiceType == typeof(IDbContextOptionsConfiguration<ProductServiceDbContext>));
if (optionsConfigurationDescriptor != null)
{
services.Remove(optionsConfigurationDescriptor);
}
// Create open SqliteConnection so EF won't automatically close it.
services.AddSingleton<DbConnection>(container =>
{
var connection = new SqliteConnection("DataSource=file::memory:?cache=shared");
connection.Open();
return connection;
});
services.AddDbContext<ProductServiceDbContext>((container, options) =>
services.AddDbContextPool<ProductServiceDbContext>((container, options) =>
{
var connection = container.GetRequiredService<DbConnection>();
options.UseSqlite(connection);
options.UseSqlite(connection).UseSnakeCaseNamingConvention();
options.ConfigureWarnings(warnings => warnings.Ignore(RelationalEventId.PendingModelChangesWarning));
});
services.Replace(ServiceDescriptor.Transient<IPermissionChecker, FakePermissionChecker>());
@@ -52,7 +66,6 @@ namespace HelloShop.ProductService.FunctionalTests.Utilities
protected override void ConfigureClient(HttpClient client)
{
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", FakeAccessTokenCreator.Create());
base.ConfigureClient(client);
}
}
@@ -7,7 +7,7 @@ using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Text;
namespace HelloShop.ProductService.FunctionalTests.Utilities
namespace HelloShop.ProductService.FunctionalTests.Helpers
{
public class FakeAccessTokenCreator
{