mirror of
https://github.com/bitwarden/server.git
synced 2025-04-18 03:28:15 -05:00
Use ParameterResolver
This commit is contained in:
parent
691afc3597
commit
19f9ab1d3c
@ -43,7 +43,7 @@ public class DatabaseDataAttribute : DataAttribute
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
theory = new ServiceTheoryDataRow(testMethod, disposalTracker, customizationContext.Services.BuildServiceProvider());
|
theory = new ServiceTheoryDataRow(testMethod, disposalTracker, customizationContext);
|
||||||
}
|
}
|
||||||
|
|
||||||
theory
|
theory
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
|
|
||||||
using Bit.Core.Enums;
|
using Bit.Core.Enums;
|
||||||
using Bit.Core.Utilities;
|
|
||||||
using Bit.Infrastructure.IntegrationTest.Services;
|
using Bit.Infrastructure.IntegrationTest.Services;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
|
||||||
@ -32,6 +31,8 @@ public class AutoMigrateAttribute : TestCustomizerAttribute
|
|||||||
// Build services provider early and run migrations
|
// Build services provider early and run migrations
|
||||||
var sp = customizationContext.Services.BuildServiceProvider();
|
var sp = customizationContext.Services.BuildServiceProvider();
|
||||||
var migrator = sp.GetRequiredService<IMigrationTesterService>();
|
var migrator = sp.GetRequiredService<IMigrationTesterService>();
|
||||||
migrator.ApplyMigration()
|
migrator.ApplyMigration();
|
||||||
|
|
||||||
|
return Task.CompletedTask;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,6 @@ using System.Reflection;
|
|||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
using Xunit.Sdk;
|
using Xunit.Sdk;
|
||||||
using Xunit.v3;
|
|
||||||
|
|
||||||
namespace Bit.Infrastructure.IntegrationTest.Utilities;
|
namespace Bit.Infrastructure.IntegrationTest.Utilities;
|
||||||
|
|
||||||
@ -10,25 +9,29 @@ public class ServiceTheoryDataRow : TheoryDataRowBase
|
|||||||
{
|
{
|
||||||
private readonly MethodInfo _testMethod;
|
private readonly MethodInfo _testMethod;
|
||||||
private readonly DisposalTracker _disposalTracker;
|
private readonly DisposalTracker _disposalTracker;
|
||||||
private readonly IServiceProvider _serviceProvider;
|
private readonly CustomizationContext _customizationContext;
|
||||||
|
|
||||||
public ServiceTheoryDataRow(MethodInfo testMethod, DisposalTracker disposalTracker, IServiceProvider serviceProvider)
|
public ServiceTheoryDataRow(MethodInfo testMethod, DisposalTracker disposalTracker, CustomizationContext customizationContext)
|
||||||
{
|
{
|
||||||
_testMethod = testMethod;
|
_testMethod = testMethod;
|
||||||
_disposalTracker = disposalTracker;
|
_disposalTracker = disposalTracker;
|
||||||
_serviceProvider = serviceProvider;
|
_customizationContext = customizationContext;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override object?[] GetData()
|
protected override object?[] GetData()
|
||||||
{
|
{
|
||||||
var parameters = _testMethod.GetParameters();
|
var parameters = _testMethod.GetParameters();
|
||||||
// Create a scope for this test
|
|
||||||
var scope = _serviceProvider.CreateAsyncScope();
|
|
||||||
|
|
||||||
var objects = new object[parameters.Length];
|
var sp = _customizationContext.Services.BuildServiceProvider();
|
||||||
|
|
||||||
|
// Create a scope for this test
|
||||||
|
var scope = sp.CreateAsyncScope();
|
||||||
|
|
||||||
|
var objects = new object?[parameters.Length];
|
||||||
for (var i = 0; i < parameters.Length; i++)
|
for (var i = 0; i < parameters.Length; i++)
|
||||||
{
|
{
|
||||||
objects[i] = scope.ServiceProvider.GetRequiredService(parameters[i].ParameterType);
|
var parameter = parameters[i];
|
||||||
|
objects[i] = _customizationContext.ParameterResolver(scope.ServiceProvider, parameter);
|
||||||
}
|
}
|
||||||
|
|
||||||
_disposalTracker.Add(scope);
|
_disposalTracker.Add(scope);
|
||||||
|
@ -16,7 +16,7 @@ public class CustomizationContext
|
|||||||
|
|
||||||
public IServiceCollection Services { get; }
|
public IServiceCollection Services { get; }
|
||||||
|
|
||||||
public Func<ServiceProvider, ParameterInfo, object?> ParameterResolver { get; set; } = DefaultParameterResolver;
|
public Func<IServiceProvider, ParameterInfo, object?> ParameterResolver { get; set; } = DefaultParameterResolver;
|
||||||
|
|
||||||
|
|
||||||
public CustomizationContext(Database database, MethodInfo testMethod, DisposalTracker disposalTracker)
|
public CustomizationContext(Database database, MethodInfo testMethod, DisposalTracker disposalTracker)
|
||||||
@ -27,7 +27,7 @@ public class CustomizationContext
|
|||||||
Services = new ServiceCollection();
|
Services = new ServiceCollection();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static object? DefaultParameterResolver(ServiceProvider services, ParameterInfo parameter)
|
private static object? DefaultParameterResolver(IServiceProvider services, ParameterInfo parameter)
|
||||||
{
|
{
|
||||||
return services.GetService(parameter.ParameterType);
|
return services.GetService(parameter.ParameterType);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user