12 .net c# xunit .net-core asp.net-core
我正在尝试在XUnit测试中为AppService实现依赖注入。理想的目标是运行原始应用程序启动/配置,并使用启动中的任何依赖项注入,而不是在测试中再次重新初始化所有DI,这就是整个目标。
更新:Mohsen的答案很接近。需要更新几个语法/要求错误才能起作用。
由于某种原因,原始应用程序可以工作,并且可以致电Department App Service。但是,它无法在Xunit中调用。最终使Testserver在原始应用程序中使用Startup和Configuration进行工作。现在收到以下错误:
Message: The following constructor parameters did not have matching fixture data: IDepartmentAppService departmentAppService
namespace Testing.IntegrationTests
{
public class DepartmentAppServiceTest
{
public DBContext context;
public IDepartmentAppService departmentAppService;
public DepartmentAppServiceTest(IDepartmentAppService departmentAppService)
{
this.departmentAppService = departmentAppService;
}
[Fact]
public async Task Get_DepartmentById_Are_Equal()
{
var options = new DbContextOptionsBuilder<SharedServicesContext>()
.UseInMemoryDatabase(databaseName: "TestDatabase")
.Options;
context = new DBContext(options);
TestServer _server = new TestServer(new WebHostBuilder()
.UseContentRoot("C:\\OriginalApplication")
.UseEnvironment("Development")
.UseConfiguration(new ConfigurationBuilder()
.SetBasePath("C:\\OriginalApplication")
.AddJsonFile("appsettings.json")
.Build()).UseStartup<Startup>());
context.Department.Add(new Department { DepartmentId = 2, DepartmentCode = "123", DepartmentName = "ABC" });
context.SaveChanges();
var departmentDto = await departmentAppService.GetDepartmentById(2);
Assert.Equal("123", departmentDto.DepartmentCode);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
Message: The following constructor parameters did not have matching fixture data: IDepartmentAppService departmentAppService
Run Code Online (Sandbox Code Playgroud)
就像真实应用程序一样,需要在测试中使用依赖注入。原始应用程序执行此操作。下面的答案目前还不够,一个使用不是当前目标的模拟,另一个答案使用绕过问题目的的Controller。
注意:IDepartmentAppService依赖于IDepartmentRepository(也注入Startup类)和Automapper依赖关系。这就是为什么调用整个启动类的原因。
良好的资源:
小智 7
使用下面的Custom Web Application Factory和ServiceProvider.GetRequiredService,随时编辑和优化答案
CustomWebApplicationFactory:
public class CustomWebApplicationFactory<TStartup> : WebApplicationFactory<TStartup> where TStartup : class
{
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.ConfigureAppConfiguration((hostingContext, configurationBuilder) =>
{
var type = typeof(TStartup);
var path = @"C:\\OriginalApplication";
configurationBuilder.AddJsonFile($"{path}\\appsettings.json", optional: true, reloadOnChange: true);
configurationBuilder.AddEnvironmentVariables();
});
// if you want to override Physical database with in-memory database
builder.ConfigureServices(services =>
{
var serviceProvider = new ServiceCollection()
.AddEntityFrameworkInMemoryDatabase()
.BuildServiceProvider();
services.AddDbContext<ApplicationDBContext>(options =>
{
options.UseInMemoryDatabase("DBInMemoryTest");
options.UseInternalServiceProvider(serviceProvider);
});
});
}
}
Run Code Online (Sandbox Code Playgroud)
整合测试:
public class DepartmentAppServiceTest : IClassFixture<CustomWebApplicationFactory<OriginalApplication.Startup>>
{
public CustomWebApplicationFactory<OriginalApplication.Startup> _factory;
public DepartmentAppServiceTest(CustomWebApplicationFactory<OriginalApplication.Startup> factory)
{
_factory = factory;
_factory.CreateClient();
}
[Fact]
public async Task ValidateDepartmentAppService()
{
using (var scope = _factory.Server.Host.Services.CreateScope())
{
var departmentAppService = scope.ServiceProvider.GetRequiredService<IDepartmentAppService>();
var dbtest = scope.ServiceProvider.GetRequiredService<ApplicationDBContext>();
dbtest.Department.Add(new Department { DepartmentId = 2, DepartmentCode = "123", DepartmentName = "ABC" });
dbtest.SaveChanges();
var departmentDto = await departmentAppService.GetDepartmentById(2);
Assert.Equal("123", departmentDto.DepartmentCode);
}
}
}
Run Code Online (Sandbox Code Playgroud)
资源:
https://docs.microsoft.com/zh-cn/aspnet/core/test/integration-tests?view=aspnetcore-2.2
https://fullstackmark.com/post/20/painless-integration-testing-with-aspnet-core-web-api
您正在混合单元测试和集成测试。TestServer
是用于集成测试的,如果您想重用Startup
类以避免再次注册依赖项,则应使用HttpClient
并通过HTTP调用使用的控制器和操作IDepartmentAppService
。
如果要进行单元测试,则需要设置DI并注册所有需要的依赖项以进行测试IDepartmentAppService
。
通过测试治具使用DI:
public class DependencySetupFixture
{
public DependencySetupFixture()
{
var serviceCollection = new ServiceCollection();
serviceCollection.AddDbContext<SharedServicesContext>(options => options.UseInMemoryDatabase(databaseName: "TestDatabase"));
serviceCollection.AddTransient<IDepartmentRepository, DepartmentRepository>();
serviceCollection.AddTransient<IDepartmentAppService, DepartmentAppService>();
ServiceProvider = serviceCollection.BuildServiceProvider();
}
public ServiceProvider ServiceProvider { get; private set; }
}
public class DepartmentAppServiceTest : IClassFixture<DependencySetupFixture>
{
private ServiceProvider _serviceProvide;
public DepartmentAppServiceTest(DependencySetupFixture fixture)
{
_serviceProvide = fixture.ServiceProvider;
}
[Fact]
public async Task Get_DepartmentById_Are_Equal()
{
using(var scope = _serviceProvider.CreateScope())
{
// Arrange
var context = scope.ServiceProvider.GetServices<SharedServicesContext>();
context.Department.Add(new Department { DepartmentId = 2, DepartmentCode = "123", DepartmentName = "ABC" });
context.SaveChanges();
var departmentAppService = scope.ServiceProvider.GetServices<IDepartmentAppService>();
// Act
var departmentDto = await departmentAppService.GetDepartmentById(2);
// Arrange
Assert.Equal("123", departmentDto.DepartmentCode);
}
}
}
Run Code Online (Sandbox Code Playgroud)
在单元测试中使用依赖注入不是一个好主意,您应该避免这种情况。顺便说一句,如果您不想重复自己的注册依赖项,则可以将DI配置包装在另一个类中,并在所需的任何位置使用该类。
通过Startup.cs使用DI:
public class IocConfig
{
public static IServiceCollection Configure(IServiceCollection services, IConfiguration configuration)
{
serviceCollection
.AddDbContext<SomeContext>(options => options.UseSqlServer(configuration["ConnectionString"]));
serviceCollection.AddScoped<IDepartmentRepository, DepartmentRepository>();
serviceCollection.AddScoped<IDepartmentAppService, DepartmentAppService>();
.
.
.
return services;
}
}
Run Code Online (Sandbox Code Playgroud)
在Startup
类和ConfigureServices
方法中只需使用IocConfig
类:
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
IocConfig.Configure(services, configuration);
services.AddMvc();
.
.
.
Run Code Online (Sandbox Code Playgroud)
如果你不想使用IocConfig
类,变化ConfigureServices
中的Startup
类:
public IServiceCollection ConfigureServices(IServiceCollection services)
{
.
.
.
return services;
Run Code Online (Sandbox Code Playgroud)
在测试项目重用IocConfig
或Startup
类中:
public class DependencySetupFixture
{
public DependencySetupFixture()
{
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", false, true));
configuration = builder.Build();
var services = new ServiceCollection();
// services = IocConfig.Configure(services, configuration)
// or
// services = new Startup(configuration).ConfigureServices(services);
ServiceProvider = services.BuildServiceProvider();
}
public ServiceProvider ServiceProvider { get; private set; }
}
Run Code Online (Sandbox Code Playgroud)
并在测试方法中:
[Fact]
public async Task Get_DepartmentById_Are_Equal()
{
using (var scope = _serviceProvider.CreateScope())
{
// Arrange
var departmentAppService = scope.ServiceProvider.GetServices<IDepartmentAppService>();
// Act
var departmentDto = await departmentAppService.GetDepartmentById(2);
// Arrange
Assert.Equal("123", departmentDto.DepartmentCode);
}
}
Run Code Online (Sandbox Code Playgroud)
小智 2
当你测试的时候。您需要使用模拟库或直接在构造函数上注入您的服务,即。
public DBContext context;
public IDepartmentAppService departmentAppService;
/// Inject DepartmentAppService here
public DepartmentAppServiceTest(DepartmentAppService departmentAppService)
{
this.departmentAppService = departmentAppService;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1758 次 |
最近记录: |